Search code examples
androidkotlinandroid-adapterbaseadapterlistadapter

java.lang.ClassCastException: CustomAdapter cannot be cast to android.widget.ArrayAdapter In Kotlin


I got the above-mentioned error when I try to create a custom adapter class in Kotlin

Source code

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()
}

}

Need Help I don't know what I did wrong.


Solution

  • I finally figure it out.. the problem was, iwas using a spinner library which only support ArrayAdapter

    the library that i used was

    com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1
    

    it only support arrayadapter