ArrayAdapter使用Kotlin android

对不起我的英语不好。 我学习kotlin,现在我需要写ArrayAdapter,但我有一些错误,当尝试做这个convertView = vi.inflate(resource, null) kotlin说我Val cannot be reassigned 。 在网站kotline我转换java类到kotlin类,并尝试做像这个网站。 为什么我有错,我该如何解决这个问题?

 open class MyAdapter(context: Context, resource: Int, list: ArrayList<MyItems>) : ArrayAdapter<MyItems>(context, resource, list) { var resource: Int var list: ArrayList<MyItems> var vi: LayoutInflater init { this.resource = resource this.list = list this.vi = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? { var holder: ViewHolder if(convertView == null){ convertView = vi.inflate(resource, null) //error in this line holder = ViewHolder() holder.image = convertView.findViewById(R.id.myImage) as ImageView? convertView.tag(holder) //error in this line } else { holder = convertView.tag as ViewHolder } return convertView } internal class ViewHolder { var image: ImageView? = null } } 

参数variables不能被重新赋值,您将需要创build一个局部variables来保存返回的视图:

 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? { var holder: ViewHolder var retView: View if(convertView == null){ retView = vi.inflate(resource, null) holder = ViewHolder() holder.image = retView.findViewById(R.id.myImage) as ImageView? retView.tag(holder) //error in this line } else { holder = convertView.tag as ViewHolder retView = convertView } return retView }