Android 回收器视图未填充数据
我有一个recycler视图,我正在将数据传递给RecycleServiceAdapter,但recycler视图不显示任何数据 ProdyCsRecycleServiceAdapter-它正在获取setList中的数据,但不显示它 请问我哪里做错了 谢谢 R 产品视图模型Android 回收器视图未填充数据,android,kotlin,android-recyclerview,android-mvvm,Android,Kotlin,Android Recyclerview,Android Mvvm,我有一个recycler视图,我正在将数据传递给RecycleServiceAdapter,但recycler视图不显示任何数据 ProdyCsRecycleServiceAdapter-它正在获取setList中的数据,但不显示它 请问我哪里做错了 谢谢 R 产品视图模型 class ProductsViewModel ( private val repository: ProductRepository, private val context: Context ): Vie
class ProductsViewModel (
private val repository: ProductRepository,
private val context: Context
): ViewModel() {
val products = repository.products
}
产品清单项目
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="10dp"
app:cardElevation="10dp" >
<LinearLayout
android:id="@+id/product_list_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/product_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@+id/product_catagory_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="catagory"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
在productsRecyclerView中,如果绑定错误,则应为ProductsListItemBinding
val binding: ProductsListItemBinding =
DataBindingUtil.inflate(layoutInflater, R.layout.products_list_item, parent, false)
ProductsMyViewHolder(val binding:ProductsListItemBinding):在适配器的setList()方法中添加notifyDataSetChanged()方法在适配器的setList()方法中添加notifyDataSetChanged()方法我不明白为什么要清除列表并同时添加它们
fun setList(products: List<Product>) {
productsList.clear()
productsList.addAll(products)
}
fun集合列表(产品:列表){
productsList.clear()
productsList.addAll(产品)
}
如果您想首先清除列表,您应该在循环之外执行此操作我不明白您为什么要清除列表并同时添加它们
fun setList(products: List<Product>) {
productsList.clear()
productsList.addAll(products)
}
fun集合列表(产品:列表){
productsList.clear()
productsList.addAll(产品)
}
如果要首先清除列表,则应在循环之外执行此操作您尚未将适配器设置为“回收器”视图。在initRecyclerView()中,在初始化适配器后设置适配器
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
//notice this
binding.productsRecyclerView.adapter = adapter
displayProductssList()
}
您尚未将适配器设置为recycler视图。在initRecyclerView()中,在初始化适配器后设置适配器
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
//notice this
binding.productsRecyclerView.adapter = adapter
displayProductssList()
}
嗨,我试过了,但没有在我的片段中工作,就在我调用setList之后,我也在做notifyDataSetChanged(),嗨,我试过了,但没有在我的片段中工作,就在我调用setList之后,我也在做notifyDataSetChanged(),非常感谢,是的,这实际上是主要问题之一。一旦我这样做了,我就发现PriductsRecycleServiceAdapter没有被正确地包含,我将
val binding:ListItemBinding
更改为val binding:ProductsListItemBinding
非常感谢,是的,这实际上是主要问题之一。一旦我这样做了,我发现PriductsRecycleServiceAdapter没有被正确地包含,我将val binding:ListItemBinding
更改为val binding:ProductsListItemBinding
private fun initRecyclerView() {
binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
//notice this
binding.productsRecyclerView.adapter = adapter
displayProductssList()
}