代码之家  ›  专栏  ›  技术社区  ›  Ajith Madhu

JAVAlang.ClassCastException:无法将CustomAdapter转换为android。小部件。Kotlin的ArrayAdapter

  •  1
  • Ajith Madhu  · 技术社区  · 7 年前

    当我尝试在Kotlin中创建自定义适配器类时,出现了上述错误

    源代码

    主要活动。千吨级

     var adapterC:CustomAdapter = CustomAdapter(this,Statearray)
     spinnerState.adapter=adapterC
    

    自定义适配器。千吨级

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

    需要帮助我不知道我做错了什么。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Ajith Madhu    7 年前

    我终于明白了。。问题是,我使用的微调器库只支持ArrayAdapter

    我使用的图书馆是

    com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1
    

    它只支持arrayadapter

        2
  •  1
  •   IntelliJ Amiya    7 年前

    ClassCastException

    抛出以指示代码试图将对象强制转换为 它不是其实例的子类。

    仅供参考

    您添加了 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)
    

    回答千吨级

    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"