'How to initialize the Reyclerview Adapter MutableList when the date picker is trigger (kotlin)

At here i simply explain the logic of the app that i create

When the user visit this activity, the user will be able to view the timeslot box that show in recyclerview, and at the top of recyclerview have a button for date picker and textview of the date

the user are allowed to click on the date picker button to choose future date

after date choose will loop to check is firestore exist the timeslot, when exist remove that particular existed slot and notifyDataSetChanged to the adapter

click on the timeslot box will navigate to another page from the help of intent

I have a requirement of the when user are click on the date picker and choose the another date, but as i said the at the previous "when exist remove that particular existed slot", my program will no show the previous slot even i try a lots of ways to initialize the slot array. In another word if previous 8:00 AM slot been removed, and the user choose another day and no existed slot in firestore, the 8:00 AM slot will still removed unless i go to another activity and come back to rendering activity again

booking_time.kt

        time = mutableListOf(
            "8:00 AM",
            "9:00 AM",
            "10:00 AM",
            "11:00 AM",
            "12:00 AM",
            "2:00 PM",
            "3:00 PM",
            "4:00 PM",
            "5:00 PM",
        )
        
        val sdf = SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH)
        val currentDate = sdf.format(Date())
        txtDate.text = currentDate
        dateBuffer = currentDate

        btnDatePicker.setOnClickListener{
            // create new instance of DatePickerFragment
            val datePickerFragment = DatePickerFragment()
            val supportFragmentManager = this.supportFragmentManager

            // we have to implement setFragmentResultListener
            supportFragmentManager.setFragmentResultListener(
                "REQUEST_KEY",
                this
            ) { resultKey, bundle ->
                if (resultKey == "REQUEST_KEY") {
                    val date = bundle.getString("SELECTED_DATE")
                    txtDate.text = date
                    if(!date.equals(dateBuffer)){
                        isDateChange = 1
                    }
                    if (date != null) {
                        dateBuffer = date
                    }
                    getTimeslotData() // when onchange also retrieve chosen date and process with array that passed by firebase again
                }
            }

            // show
            datePickerFragment.show(supportFragmentManager, "DatePickerFragment")
        }

        getTimeslotData() //init

        adapter = TimeslotAdapter(timeslotArrayList)
        recyclerView.adapter = adapter

    private fun getTimeslotData(){
        if(isDateChange!=-1) {
            time.clear()
            time = mutableListOf(
                "8:00 AM",
                "9:00 AM",
                "10:00 AM",
                "11:00 AM",
                "12:00 AM",
                "2:00 PM",
                "3:00 PM",
                "4:00 PM",
                "5:00 PM",
            )
            isDateChange = -1
        }
        timeslotArrayList.clear()
        for (i in time.indices) {
            val times = timeModel(time[i])
            timeslotArrayList.add(times)
        }

        val date = getCurrentDateTime()



        db = FirebaseFirestore.getInstance()
        db.collection("booking").whereEqualTo("hairdresserChoose",hairdresserNameBuffer).whereEqualTo("dateBook",dateBuffer).get()
            .addOnSuccessListener { documents ->

                for (document in documents) {
                    firebaseBookingResultArrayList.add(document.get("timeBook").toString())
                    Log.i("code123 Get data",document.get("timeBook").toString())
                }
                if(firebaseBookingResultArrayList.size > 0){
                    for( i in firebaseBookingResultArrayList.indices.reversed()){
                        for(j in time.indices.reversed()){
                            if(firebaseBookingResultArrayList.get(i).equals(time.get(j))){
                                time.removeAt(j)
                                timeslotArrayList.removeAt(j)
                                Log.i("code123 one removed -1","178")
                            }
                            if(dateBuffer.equals(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy",Locale.ENGLISH)))) {
                                if(SimpleDateFormat("hh:mm a",Locale.ENGLISH).parse(time.get(j)).before(Date(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm a",Locale.ENGLISH))))){
                                    time.removeAt(j)
                                    timeslotArrayList.removeAt(j)
                                    Log.i("code123 one removed -2","183")
                                }
                            }
                        }
                    }
                    adapter.notifyDataSetChanged()
                }
            }
            .addOnFailureListener { exception ->
                Log.e("code123 Error getting documents:", exception.toString())
            }
    }
}

TimeslotAdapter.kt

package com.example.salon_appointment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TimeslotAdapter(private val timeList: MutableList<timeModel>) :
    RecyclerView.Adapter<TimeslotAdapter.timeslotViewHolder>() {

    private lateinit var mListener : chooseServiceAdapter.onItemClickListener

    interface onItemClickListener{
        fun onItemClick(view : View)
    }

    fun setOnClickListener(listener: chooseServiceAdapter.onItemClickListener){
        mListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): timeslotViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.timeslot_layout,parent,false)
        return timeslotViewHolder(itemView,mListener)
    }

    override fun onBindViewHolder(holder: timeslotViewHolder, position: Int) {
        val currentItem = timeList[position]
        holder.time.text = currentItem.time
    }

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

    class timeslotViewHolder(itemView: View, listener: chooseServiceAdapter.onItemClickListener) : RecyclerView.ViewHolder(itemView){
        val time : TextView = itemView.findViewById(R.id.txtTime)

        init {
            itemView.setOnClickListener{
                listener.onItemClick(itemView)
            }
        }
    }
}

timeModel.kt

package com.example.salon_appointment

data class timeModel(var time : String){
}

And then i think this will be another question i guess, but can i also asking how to make the slot will auto removed when the activity is redering? such as in default will display today as the date and the function i want to achieve is when user visit to the activity at 6:00 PM which already no slot to be choose(the lastest slot only 5:00 PM), can i make the recyclerview will show nothing, unless the user choose another date, and if visit at 4:00 PM will only show 5:00 PM slot(unless visit at 3:59 PM will show 4:00 PM slot)

As you can see at the booking_time.kt i have

if(dateBuffer.equals(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy",Locale.ENGLISH)))) {
                                if(SimpleDateFormat("hh:mm a",Locale.ENGLISH).parse(time.get(j)).before(Date(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm a",Locale.ENGLISH))))){
                                    time.removeAt(j)
                                    timeslotArrayList.removeAt(j)
                                    Log.i("code123 one removed -2","183")
                                }
                            }

These code to read current date for the buffer, and any onclick on date picker button will update the datebuffer.After that will read the time, i have Log the value out and it is same with the array of time that i created, but then, these code not functioning...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source