0

i have a list that display data depends on which which list you chose i am getting this list from API but the problem here the API list doesn't provide "All" to display everything inside the list

Code:

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




                    }
                })
        }

i tried the solution from this question: How to add an item to a list in Kotlin?

and added this:

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

but it didn't work, what am i doing wrong?

this is the adapter:

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?)


}

}

and the data class:

class ilmcategoriesData (
val id: Int,
val name: String
    )
Enigma
  • 353
  • 4
  • 14
  • 4
    I'm not sure where you're having the issue in your first code sample. However in regards to this: `list.toMutableList().add(original)`, remember that this creates a **new** list and adds `original`. So `list` will be unmodified. – Henry Twist Jul 04 '21 at 11:14
  • so what is the correct way to add item to the list? – Enigma Jul 04 '21 at 11:17
  • 1
    If it's a non-mutable list, then you namely can't add an item to it, you can only make a copy and add an item to that. Without any code of your issue it's difficult to determine. – Henry Twist Jul 04 '21 at 11:28
  • i thought that i'm able to do it through that code, i have added the adapter and the data class to my question – Enigma Jul 04 '21 at 11:34
  • You can set `countryList` to your new list. However I still don't see where you're trying to add the `original` element in your example. If you need any more help you can produce a [mre] and I would be happy to take a look! – Henry Twist Jul 04 '21 at 21:21

1 Answers1

0

Solution in adapter:

init {
    val arrayList = ArrayList<ilmcategoriesData>()
    arrayList.addAll(countryList)
    arrayList.add(0, ilmcategoriesData(41, "All Categories"))
    arrayList.add(1, ilmcategoriesData(42, "Favorite"))
    countryList = arrayList
}
Enigma
  • 353
  • 4
  • 14