more
more

Reputation: 133

RecyclerView Adapter cannot work

Question is simple, I used recyclerview adapter and set at mainactivity.A pplication started but adapter field just white blank. No error. When I was debugging, I saw Adapter constructor method work but onBindViewHolder didn't work.

MainActivity OnCreate Method

@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();     
   ...
}

Upvotes: 0

Views: 78

Answers (1)

user9906968
user9906968

Reputation:

You set the content of the adapter to product list and later you replace that variable by dbHelper.getAllProducts()

Solution 1: Use productList.addAll(dbHelper.getAllProducts())

Solution 2: Add a setter for the content to the adapter and use adapter.setData(dbHelper.getAllProducts()) or add adapter.setData(productList) at the end

Upvotes: 1

Related Questions