Home > other >  How to use MutableLiveData<List<String>> as Data of RecyclerView Adapter?
How to use MutableLiveData<List<String>> as Data of RecyclerView Adapter?

Time:01-06

I have one RecyclerView, and if I click one item of it, I want make Data of RecyclerView change.

companion object {
    var regionData: MutableLiveData<List<String>> = MutableLiveData()
    var smallRegionScreen : Boolean = false
}

So I use MutableLiveData to make Data mutable and keep being observed.

    adapter = regionData.value?.let { RegionAdapter(this, it, smallRegionScreen) }!!

I pass regionData.value as Data of Adapter, whose type will be List. And smallRegionScreen is Boolean value.

Since first click of item and second click of item in RecyclerView's taken action will be different, so I differentiate it by this value.

 regionDB.get()
            .addOnSuccessListener { documents ->
                for (document in documents) {
                    var newArray = ArrayList<String>()
                    Log.d("리지온1", "$document")
                    for ((k, v) in document.data) {
                        regionData.value.add(v.String)
                        Log.d("리지온", "${regionData.value}")
                    }
                }
                adapter.notifyDataSetChanged()
            }
        binding.regionRecycler.adapter=adapter
        binding.regionRecycler.layoutManager= LinearLayoutManager(this)
    }

As here, I add item to regionData.value. But it shows empty Array.

What is problem here?

And My Adapter is below, my process is okay?

class RegionAdapter(private var context: Context, private var regionData:  List<String>, private var smallRegionScreen: Boolean): RecyclerView.Adapter<RegionAdapter.RegionViewHolder>() {

var userDB = Firebase.firestore.collection("users")
var userId = Firebase.auth.currentUser?.uid

companion object {
    var REGION_RECYCLER_CLICKED = "com.chungchunon.chunchunon_android.REGION_RECYCLER_CLICKED"
}

inner class RegionViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {

    val regionView: TextView = itemView.findViewById(R.id.regionSelectText)

    fun bind (position: Int) {
        regionView.text =  regionData[position]
    }
}

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

override fun onBindViewHolder(holder: RegionViewHolder, position: Int) {
    holder.bind(position)

    holder.itemView.setOnClickListener { view ->

        if(!smallRegionScreen) {
            var selectedRegion = regionData[position]
            var regionSet = hashMapOf(
                "region" to selectedRegion
            )
            userDB.document("$userId").set(regionSet)

             var regionDB = Firebase.firestore.collection("region")

            regionDB
                .document("4ggk4cR82mz46CjrLg60")
                .collection(selectedRegion.toString())
                .get()
                .addOnSuccessListener { documents ->
                    for (document in documents) {
                        for ((k, v) in document.data) {
                            regionData.plus(v.toString())
                        }
                    }
                    smallRegionScreen = true
                }

        } else {
            var selectedSmallRegion = regionData[position]
            var regionSet = hashMapOf(
                "smallRegion" to selectedSmallRegion
            )
            userDB.document("$userId").set(regionSet)

        }
    }
}

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

CodePudding user response:

If you want to add data to your MutableLiveData:

val regionDataList = regionData.value

val templateList = mutableListOf<String>()
regionDataList?.forEach { data ->
    templateList.add(data)
   }

templateList.add(v.String)

regionData.value = templateList

CodePudding user response:

you can add data in the list like this :-

regionData.value.add(v.toString())

hope this answer you like it. and upvote and approve the answer

  • Related