如何将项目添加到列表中以显示kotlin中的所有项目?

s4chpxco  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(277)

我有一个列表,显示数据取决于您选择的列表我从api获取此列表,但这里的问题是api列表没有提供“全部”来显示列表中的所有内容
代码:

val findercatService = findercatService.create().getServices()
    findercatService.enqueue(object : Callback<List<ilmcategoriesData>> {
        override fun onResponse(
            call: Call<List<ilmcategoriesData>>?,
            response: Response<List<ilmcategoriesData>>?
        ) {
            var list = response?.body()!!

            Log.e("XXimmmm2XXwwwww", list.toString())
            // recyclerView3.adapter = ilmcategoriesAdapter(list, MainActivity())
            recyclerView3.adapter =
                ilmcategoriesAdapter(list, MainActivity(), object : ilmcategoriesAdapter.OnClickRecyclerChild {
                    override fun myCallback(s: String?) {

                        Log.e("fsds","5454556666")
                        Log.d(TAG, s!!)

                        when (s.toInt()) {
                            26 -> AppPreferences.heightInCentimeters = 1
                            27 -> AppPreferences.heightInCentimeters = 2
                            28 -> AppPreferences.heightInCentimeters = 3
                            29 -> AppPreferences.heightInCentimeters = 4
                            30 -> AppPreferences.heightInCentimeters = 5
                            31 -> AppPreferences.heightInCentimeters = 6
                            32 -> AppPreferences.heightInCentimeters = 7
                            33 -> AppPreferences.heightInCentimeters = 8
                            34 -> AppPreferences.heightInCentimeters = 9
                            35 -> AppPreferences.heightInCentimeters = 10
                            36 -> AppPreferences.heightInCentimeters = 11
                            37 -> AppPreferences.heightInCentimeters = 12
                            38 -> AppPreferences.heightInCentimeters = 13
                            39 -> AppPreferences.heightInCentimeters = 14
                            else -> { // Note the block
                               AppPreferences.heightInCentimeters = 0
                            }
                        }

                        val finderService = finderService.create().getServices()
                        finderService.enqueue(object : Callback<List<ilmFinders>> {
                            override fun onResponse(
                                call: Call<List<ilmFinders>>?,
                                response: Response<List<ilmFinders>>?
                            ) {
                                val list = response?.body()!!

                                var newAdapter = IimfinderAdapter(list, MainActivity())
                                Log.e("XXimmmm2XX", list.count().toString())
                                recyclerView6.adapter = IimfinderAdapter(list, MainActivity())

                                newAdapter.notifyDataSetChanged()
                            }

                            override fun onFailure(call: Call<List<ilmFinders>>?, t: Throwable?) {
                                Log.e("dsfdfs", "daily")
                            }
                        })

                    }
                })
        }

我尝试了这个问题的解决方案:如何在kotlin中向列表添加项目?
并补充说:

val original = ilmcategoriesData(0, "All")
     list.toMutableList().add(original)

但它不起作用,我做错了什么?
这是适配器:

class ilmcategoriesAdapter(var countryList: List<ilmcategoriesData>, var activity: MainActivity, var listener: OnClickRecyclerChild): RecyclerView.Adapter<ilmcategoriesAdapter.ViewHolder>(){
lateinit var context: Context

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ilmcategoriesAdapter.ViewHolder {
    context = parent.context!!
    val view = LayoutInflater.from(context).inflate(R.layout.list_item3, parent, false)
    return ViewHolder(view)
}

override fun getItemCount(): Int {
    return countryList.size
}

override fun onBindViewHolder(holder: ilmcategoriesAdapter.ViewHolder, position: Int) {

    holder.eventName.text = countryList[position].name
    Log.d("ggggggggggggggg", countryList.toString())

    holder.eventName.setOnClickListener {

      var CategoryID =   countryList[position].id
        var CategoryIdStr = CategoryID.toString()

        Log.e("fsds","fdfsd32")

        AppPreferences.heightInCentimeters = CategoryID
        listener.myCallback(CategoryIdStr)
    }

}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    val eventName: TextView = itemView.findViewById(R.id.cat_title)

}

interface OnClickRecyclerChild {
    fun myCallback(s: String?)

}

}

以及数据类:

class ilmcategoriesData (
val id: Int,
val name: String
    )

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题