java.lang.ClassCastException:CustomAdapter 无法在 Kotlin 中转换为 android.widget.ArrayAdapter
java.lang.ClassCastException: CustomAdapter cannot be cast to android.widget.ArrayAdapter In Kotlin
当我尝试在 Kotlin 中创建自定义适配器 class 时出现上述错误
源代码
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()
}
}
需要帮助我不知道我做错了什么。
Thrown to indicate that the code has attempted to cast an object to a
subclass of which it is not an instance.
仅供参考
您添加了 Multiple Adapter
。删除第二个。
不要
class CustomAdapter(val activity: Activity,val array:JSONArray) : BaseAdapter(), ListAdapter
做
class CustomAdapter(context: Context,var arrayLIST: ArrayList<Response>) : BaseAdapter() {
演示版
var arrayLIST: ArrayList<Response>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
arrayLIST=ArrayList()
val jsonObj = ("[{\"Id\":\"35\",\"Name\":\"Kerala\"},{\"Id\":\"36\",\"Name\":\"Tamilnadu\"}]")
val jo = JSONArray(jsonObj)
val num = 0 until jo.length()
for (i in num) {
val loanObj = jo.getJSONObject(i)
val Id = loanObj.getString("Id")
val Name = loanObj.getString("Name")
arrayLIST!!.add(Response(Id,Name))
}
var adapterC:CustomAdapter = CustomAdapter(this@MainActivity,arrayLIST)
Response.kt
data class Response
(
@SerializedName("id") val id : String,
@SerializedName("name") val name : String
)
注意
确保添加,
implementation "com.google.code.gson:gson:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
我终于弄明白了。问题是,我使用的是仅支持 ArrayAdapter 的微调器库
我使用的库是
com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1
仅支持数组适配器
当我尝试在 Kotlin 中创建自定义适配器 class 时出现上述错误
源代码
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()
}
}
需要帮助我不知道我做错了什么。
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
仅供参考
您添加了 Multiple Adapter
。删除第二个。
不要
class CustomAdapter(val activity: Activity,val array:JSONArray) : BaseAdapter(), ListAdapter
做
class CustomAdapter(context: Context,var arrayLIST: ArrayList<Response>) : BaseAdapter() {
演示版
var arrayLIST: ArrayList<Response>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
arrayLIST=ArrayList()
val jsonObj = ("[{\"Id\":\"35\",\"Name\":\"Kerala\"},{\"Id\":\"36\",\"Name\":\"Tamilnadu\"}]")
val jo = JSONArray(jsonObj)
val num = 0 until jo.length()
for (i in num) {
val loanObj = jo.getJSONObject(i)
val Id = loanObj.getString("Id")
val Name = loanObj.getString("Name")
arrayLIST!!.add(Response(Id,Name))
}
var adapterC:CustomAdapter = CustomAdapter(this@MainActivity,arrayLIST)
Response.kt
data class Response
(
@SerializedName("id") val id : String,
@SerializedName("name") val name : String
)
注意
确保添加,
implementation "com.google.code.gson:gson:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
我终于弄明白了。问题是,我使用的是仅支持 ArrayAdapter 的微调器库
我使用的库是
com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1
仅支持数组适配器