'How can I update my recyclerView using firecloud? I have an error, I cant initialize binding again

My Adapter class is:

class CategoryAdapter(private val categoryList: List):RecyclerView.Adapter<CategoryAdapter.CustomViewHolder>() {

inner class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view)

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

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
    val category = categoryList[position]
    val view = holder.itemView
    holder.itemView.context

    val background = view.findViewById<ImageView>(R.id.categoryBackground)
    val img = view.findViewById<ImageView>(R.id.categoryImg)

    if (category.Background.isNotEmpty()) {
        var buttonDrawable = background.background
        buttonDrawable = DrawableCompat.wrap(buttonDrawable)
        DrawableCompat.setTint(buttonDrawable, Color.parseColor(category.Background.toString()))
        background.background = buttonDrawable
    } else {
        background.setImageDrawable(R.id.notification.toDrawable())
    }

    if (category.Img.isNotEmpty()) {
        Picasso.get().load(category.Img).into(img)
    }

    view.setOnClickListener {
        
        ProductsFragment().setCategoryType(category.Nombre)
        notifyDataSetChanged()
    }
}

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

}

and the fragment is: class ProductsFragment : Fragment() {

private lateinit var binding: FragmentProductsBinding
private var db = FirebaseFirestore.getInstance()

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = FragmentProductsBinding.inflate(layoutInflater)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    category()

    products()

}

fun setCategoryType(name:String) {
    db.collection("Productos").whereEqualTo("Categoria", name).get().addOnSuccessListener { documents ->
        val productList = mutableListOf<ProductsDataClass>()
        for (document in documents) {
            val productObject = document.toObject(ProductsDataClass::class.java)
            productList.add(productObject)
            binding.productsRecyclerView.adapter = ProductsAdapter(productList)
            binding.productsRecyclerView.layoutManager = GridLayoutManager(context, 3)
        }
    }.addOnFailureListener { exception ->
        Log.w("Error", "Error getting documents: ", exception)
        Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show()
    }

}

private fun products() {
    db.collection("Productos").get().addOnSuccessListener { documents ->
        val productList = mutableListOf<ProductsDataClass>()
        for (document in documents) {
            val productObject = document.toObject(ProductsDataClass::class.java)
            productList.add(productObject)
            binding.productsRecyclerView.adapter = ProductsAdapter(productList)
            binding.productsRecyclerView.layoutManager = GridLayoutManager(context, 3)
        }
    }.addOnFailureListener { exception ->
        Log.w("Error", "Error getting documents: ", exception)
        Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show()
    }
}

private fun category() {
    db.collection("Categoria").get().addOnSuccessListener { documents ->
        val categoryList = mutableListOf<CategoryDataClass>()
        for (document in documents) {
            val categoryObject = document.toObject(CategoryDataClass::class.java)
            categoryList.add(categoryObject)
            binding.categoryRecyclerView.adapter = CategoryAdapter(categoryList)
            binding.categoryRecyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        }
    }.addOnFailureListener { exception ->
        Log.w("Error", "Error getting documents: ", exception)
        Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show()
    }
}

}

I want when you pick an element from category, you update the recycler view with the elements that has this name on category, but my recycler view doesnt update



Solution 1:[1]

Here, you are calling setCategoryType(category.Nombre) in a new instance of ProductsFragment, not in your current fragment instance. You can either send a reference of current fragment in your Adapter, or use an interface or callback which will update the data when getting called from within adapter.
For example, using callback

class CategoryAdapter(
    private val categoryList: List<CategoryDataClass>,
    private val onCategorySelected: (CategoryDataClass) -> Unit
): RecyclerView.Adapter<CategoryAdapter.CustomViewHolder>(){
    ...// Skipping rest of the code

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val category = categoryList[position]
        val view = holder.itemView
        holder.itemView.context
        ...// Skipping rest of the code

        view.setOnClickListener {

            onCategorySelected(category)
        }
    }
}

And in your fragment class, where you are initializing this CategoryAdapter, change it to something like that:

binding.categoryRecyclerView.adapter = CategoryAdapter(categoryList) { category ->
    setCategoryType(category.Nombre)
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Ahsan Ullah Rasel