在ArrayAdaper中,指定为非null的参数为null

时间:2016-02-25 08:09:27

标签: android kotlin

我为Spinner扩展了ArrayAdapter:

#!/bin/bash

imageFiles=( *.img );
for i in ${imageFiles[*]}
do
    withoutExtension=${i%.img};
    cp data.txt "$withoutExtension.txt";
done

我得到例外:

class OrderAdapter(context: Context, resource: Int, objects: List<Order>) : ArrayAdapter<Order>(context, resource, objects) {
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val view = super.getView(position, convertView, parent)
            view?.let { view.find<TextView>(android.R.id.text1).text = getItem(position).name }
            return view
        }
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val view = super.getDropDownView(position, convertView, parent)
            view?.let {view.find<TextView>(android.R.id.text1).text = getItem(position).name }
            return view
        }
    }

任何想法如何解决?

4 个答案:

答案 0 :(得分:7)

此问题是由增量编译引起的。只需要在从Java转换后重建项目。

答案 1 :(得分:3)

请查看以下示例:

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View? {
    return getCustomView(position, convertView, parent)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
    return getCustomView(position, convertView, parent)
}

fun getCustomView(position: Int, convertView: View?, parent: ViewGroup): View? {
    val rootView: View? = LayoutInflater.from(context).inflate(R.layout.my_custom_view, parent, false)
    return rootView
}

返回的值应该是可以为空的。

答案 2 :(得分:0)

就我而言,我只需要添加“?”到convertView参数

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {

答案 3 :(得分:-2)

override fun getView(i: Int, view: View, parent: ViewGroup): View {

var itemView = LayoutInflater.from(context).inflate(R.layout.item_dashbord_data, parent, false)

        return itemView
    }

//更改View对象的名称(itemView)与getView第二个对象(视图)不同。所以将视图更改为itemView或其他名称