如何使用FireCloud更新我的Recyclerview?我有一个错误,我不能再次初始化绑定

发布于 2025-01-31 08:04:43 字数 4382 浏览 2 评论 0原文

我的适配器类是:

类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
}

}

,片段为:: class products fragment: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()
    }
}

}

我想要当您从类别中选择一个元素时,您会在类别上使用此名称的元素更新回收器视图,但是我的recycler视图没有更新

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2025-02-07 08:04:43

在这里,您正在调用setCategoryType(category.nombre)productsfragment fragment的新实例中,而不是在您当前的片段实例中。您可以在适配器中发送当前片段的引用,也可以使用接口或回调,该接口或回调将在适配器内获取时更新数据。

例如,使用回调

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)
        }
    }
}

和片段类,在您初始化此类别的情况下,将其更改为类似的东西:

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

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文