[英]RecyclerView Adapter cannot work
问题很简单,我使用了recyclerview适配器并将其设置为mainactivity。开始复制,但适配器字段为白色空白。 没错 在调试时,我看到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:将内容的设置器添加到适配器,然后使用adapter.setData(dbHelper.getAllProducts())
或在末尾添加adapter.setData(productList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.