如何使用FireCloud更新我的Recyclerview?我有一个错误,我不能再次初始化绑定
我的适配器类是:
类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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您正在调用
setCategoryType(category.nombre)
在productsfragment fragment
的新实例中,而不是在您当前的片段实例中。您可以在适配器中发送当前片段的引用,也可以使用接口或回调,该接口或回调将在适配器内获取时更新数据。例如,使用回调
和片段类,在您初始化此类别的情况下,将其更改为类似的东西:
Here, you are calling
setCategoryType(category.Nombre)
in a new instance ofProductsFragment
, 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
And in your fragment class, where you are initializing this CategoryAdapter, change it to something like that: