android - RecyclerView 适配器无法工作
问题描述
问题很简单,我使用了 recyclerview 适配器并设置在 mainactivity.A 应用程序启动但适配器字段只是白色空白。没有错误。当我调试时,我看到 Adapter 构造方法工作但 onBindViewHolder 没有工作。
MainActivity OnCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
...
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
productList = new ArrayList<>();
productAdapter = new ProductAdapter(this,productList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
productList = dbHelper.getAllProducts();
productAdapter.notifyDataSetChanged();
...
}
解决方案
您将适配器的内容设置为产品列表,然后将该变量替换为dbHelper.getAllProducts()
解决方案 1:使用productList.addAll(dbHelper.getAllProducts())
解决方案2:将内容的setter添加到适配器并在最后使用adapter.setData(dbHelper.getAllProducts())
或添加adapter.setData(productList)
推荐阅读
- java - 忽略加载急切的一对一休眠
- python - ImportError:无法从“tensorflow.keras.preprocessing”(未知位置)导入名称“image_dataset_from_directory”
- html - 如何在引导工具提示中显示表单元素?
- c# - 如何比较数组以查看元素重复了多少次?
- arrays - 如何在Scala中以元素方式将两个嵌套数组缓冲区相乘?
- java - 我的paintComponent 绘制了我没有编码的东西
- javascript - ReactJS axios.post 通过返回对象的数组函数在地图内
- java - 我是否需要为我的 Spring Data JDBC 项目配置下面示例中的 bean?
- node.js - DynamoDB - 根据排序键上的 ConditionExpression 删除项目
- javascript - js - 根据条件从嵌套数组中删除数组