【发布时间】:2018-05-16 05:16:02
【问题描述】:
当我尝试在 Kotlin 中创建自定义适配器类时遇到上述错误
源代码
MainActivity.kt
var adapterC:CustomAdapter = CustomAdapter(this,Statearray)
spinnerState.adapter=adapterC
CustomAdapter.kt
class CustomAdapter(val activity: Activity,val array:JSONArray) : BaseAdapter(), ListAdapter
{
lateinit var ItemName: TextView
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
var view=convertView
if (view == null)
view = activity.layoutInflater.inflate(R.layout.spinnerlayout, null)
try {
ItemName = view?.findViewById(R.id.ItemName) as TextView
val obj = array.getJSONObject(position)
ItemName.setText(obj.getString("Name"))
view.setTag(obj.getString("Id"))
} catch ( e:JSONException) {
Log.e("At Custom Class",e.toString())
}
return view
}
override fun getItem(position: Int): JSONObject {
return array.optJSONObject(position)
}
override fun getItemId(position: Int): Long {
var jsonObject=getItem(position)
return jsonObject.optLong("id")
}
override fun getCount(): Int {
return array.length()
}
}
需要帮助我不知道我做错了什么。
【问题讨论】:
-
你在哪里设置适配器?发布该代码
-
您的
CustomAdapter没有扩展ArrayAdapter。理解:大象不能被扔给鸟,因为它不会延伸鸟。虽然鹰可以被投射到鸟身上,因为它确实延伸了鸟。
标签: android kotlin android-adapter baseadapter listadapter