dimanche 17 février 2019

Keep RecylerView adapter checkbox checked if user come back to the activity by clicking on same button[Kotlin]

I have activity one where i need to click user has to select location from list of location so for that when user click on select location text view it will open list of location with checkbox.

When user select location(can select multiple location) and click on done then all selected location will be showing on activity one at textView with all selected checked text view value now when user click on same textview to add more location i'm not getting all previous selected checkbox.

I'm not getting how to achieve this. I need all old checkbox should be selected and user can select some more new checkbox if click on same textview. Please help me to get this. Java code will be also helpful for me

Below is my recylerView Adapter code:-

    class SelectMedicineAdapter (val medicineList : ArrayList<String>, val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>()  {
    var selectedCheckBoxMedicineList : ArrayList<String> = ArrayList()

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

        holder.itemView.textViewSelectMedicineName.text = medicineList.get(position)

        holder.itemView.checkboxSelectMedicine.setOnCheckedChangeListener { buttonView, isChecked ->
            val itemText = medicineList.get(position)

            if (isChecked) {
                selectedCheckBoxMedicineList.add(itemText)

            } else {
                selectedCheckBoxMedicineList.remove(itemText)
            }


        }
    }

    fun getSelectedMedicineList(): ArrayList<String> {
        return selectedCheckBoxMedicineList
    }

    override fun getItemCount(): Int {

        return medicineList.size
    }

    override fun onCreateViewHolder(holder: ViewGroup, p1: Int): RecyclerView.ViewHolder {

        val v= (LayoutInflater.from(context).inflate(R.layout.row_select_medicine_adapter,holder,false))
        return ViewHolder(v)
    }

    class ViewHolder (itemView: View): RecyclerView.ViewHolder(itemView){

        var textViewSelectMedicineName = itemView.textViewSelectMedicineName
        var imageViewPlusButton = itemView.imageViewPlusButton
        var imageViewMinusButton = itemView.imageViewMinusButton
        var checkboxSelectMedicine = itemView.checkboxSelectMedicine

    }


}




Aucun commentaire:

Enregistrer un commentaire