How to update items using list adapter in ChildEventListener

58 views Asked by At

I am working on a project. And I am trying to show a list of object in which data is fetched from Firebase Realtime Database. When I change the data on Firebase console/make some changes in database, the changes are reflected when activity is re-created and not on realtime basis. I used list adapter since in recycler view, notifyDatasetChanged() is very expensive

//initialise data
database = FirebaseDatabase.getInstance()
categoryList = arrayListOf()

//set up adapter and recycler view
val rvCategory: RecyclerView = binding.rvCategory
val categoryAdapter = CategoryAdapter()
rvCategory.layoutManager = LinearLayoutManager(this@CategoryListActivity)
rvCategory.adapter = categoryAdapter
val databaseReference = database.getReference("category")
databaseReference.addChildEventListener(object: ChildEventListener {
    override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
        //adding the data in list
        val data = snapshot.getValue(Category::class.java)
        categoryList.add(data!!)
        //sends the data to the recycler view
        categoryAdapter.submitList(categoryList)
    }

    override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {
        //adding the data in list
        val data = snapshot.getValue(Category::class.java)
        categoryList.add(data!!)
        //sends the data to the recycler view
        categoryAdapter.submitList(categoryList)
    }

    override fun onChildRemoved(snapshot: DataSnapshot) {
        //adding the data in list
        val data = snapshot.getValue(Category::class.java)
        categoryList.add(data!!)
        //sends the data to the recycler view
        categoryAdapter.submitList(categoryList)
    }

    override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {
        //adding the data in list
        val data = snapshot.getValue(Category::class.java)
        categoryList.add(data!!)
        //sends the data to the recycler view
        categoryAdapter.submitList(categoryList)
    }

    override fun onCancelled(error: DatabaseError) {
        Toast.makeText(this@CategoryListActivity, error.message,Toast.LENGTH_SHORT).show()
    }
})

Adapter for category list

class CategoryAdapter: ListAdapter<Category, CategoryAdapter.CategoryViewHolder>(DiffCallBack) {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CategoryViewHolder {
        return CategoryViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_category_list,parent,false )
        )
    }

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.bind(getItem(position))
        holder.itemView.setOnClickListener {
            val intent = Intent(holder.itemView.context,CategoryDoctorListActivity::class.java)
            val bundle = Bundle()
            bundle.putString("categoryName",getItem(position).cName)
            intent.putExtras(bundle)
            startActivity(holder.itemView.context,intent,bundle)
        }
    }

    class CategoryViewHolder(view: View) : RecyclerView.ViewHolder(view){
        private val categoryName: TextView = view.findViewById(R.id.category_name)
        private val categoryImage: ImageView = view.findViewById(R.id.category_image)

        fun bind(categoryData: Category){
            categoryName.text = categoryData.cName
            Glide.with(itemView.context).load(categoryData.cPhoto).into(categoryImage)
        }
    }

    companion object{
        private val DiffCallBack = object: DiffUtil.ItemCallback<Category>(){
            override fun areItemsTheSame(oldItem: Category, newItem: Category): Boolean {
                return oldItem.cId == newItem.cId
            }

            override fun areContentsTheSame(oldItem: Category, newItem: Category): Boolean {
                return areItemsTheSame(oldItem,newItem)
            }

        }
    }

}

I have used a list adapter in which we are provided a function submit list, ref: check documentation for the function

Checked some logs, OnChildChanged is called when there is a change in a dataset but the same is not reflected in the list using recycler view

Firebase Realtime Database Screenshot

Suggest changes for the same

0

There are 0 answers