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.

2 answers

  • answered 2018-05-16 05:23 IntelliJ Amiya

    ClassCastException

    Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

    FYI

    You added Multiple Adapter. Remove 2nd One.

    Don't

    class CustomAdapter(val activity: Activity,val array:JSONArray) : BaseAdapter(), ListAdapter
    

    Do

    class CustomAdapter(context: Context,var arrayLIST: ArrayList<Response>) : BaseAdapter() {
    

    DEMO

    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
    )
    

    NOTE

    Make sure add,

       implementation "com.google.code.gson:gson:2.3.0"
        implementation "com.squareup.retrofit2:converter-gson:2.3.0"
    

  • answered 2018-05-16 06:40 Ajith Madhu

    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