Shir K
Shir K

Reputation: 613

refresh the whole RecyclerView from within the Adapter

I try to refresh the whole RecyclerView from within the Adapter class (onBindViewHolder method). If I click on an item at the list, I would like this item parameters would change. What is the best way to do so?

onBindViewHolder method in Adapter class:

@Override
public void onBindViewHolder(@NonNull final StoreRecyclerViewAdapter.ViewHolder holder, final int position) {

    // Get the image of the Player item
    Glide.with(context)
            .asBitmap()
            .load(allPlayers.get(position))
            .into(holder.playerInStore);

    // Get the price of the Player of the item
    holder.playerPrice.setText(allPrices.get(position).toString());

    // Get the status of the Player of the item
    holder.status = allStatus.get(position);

    // If Player status is "CLOSED", get the closed lock image of the item
    if (allStatus.get(position).equals("CLOSE")) {
        Glide.with(context)
                .asBitmap()
                .load(close.getStatusPic())
                .into(holder.playerLock);

        // The user can purchase the item
        holder.layoutStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int price = Integer.valueOf(holder.playerPrice.getText().toString());
                boolean canPurchase = StoreLogic.getStoreLogic().canPurchase(price);

                if (canPurchase) {

                    String purchaseSuccessfully = "purchase succcessfully :)";
                    Toast.makeText(context, purchaseSuccessfully, Toast.LENGTH_SHORT).show();

                    // update list details
                    //****************** REFRESH LIST *******************



                }
                else {
                    String purchaseFailed = "not enough coins :(";

                    Toast.makeText(context, purchaseFailed, Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

    // If Player status is "OPEN", get the open lock image of the item
    else {
        Glide.with(context)
                .asBitmap()
                .load(open.getStatusPic())
                .into(holder.playerLock);

        // The user cannot purchase the item
        holder.layoutStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "already available", Toast.LENGTH_SHORT).show();

            }
        });
    }
}

initRecyclerView method in RecyclerView class:

protected void initRecyclerViewForAllPlayers() {
    RecyclerView recyclerView = findViewById(R.id.rv_store);
    StoreRecyclerViewAdapter adapter = new StoreRecyclerViewAdapter(this, allPlayers, allPrices, allStatus, open, close);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager((this)));

}

FOUND THE SOLUTION

Unfortunately, neither of the solutions in the comments helped, the methods notifyDataSetChanged() , notifyItemChanged(position) did not reffreshed the data.

Maybe it will help someone in the future: In order to refresh the whole list, I access to the init method (initImageBitmapsForAllPlayers) in the Activity (Store), and call it from onBindViewHolder in the Adapter:

if (context instanceof Store) {
    notifyItemChanged(position);
    ((Store) context).initImageBitmapsForAllPlayers();
}

Upvotes: 1

Views: 368

Answers (5)

Sead Lab
Sead Lab

Reputation: 211

Try this:

RecyclerView recyclerView = findViewById(R.id.rv_store);
recyclerView.setLayoutManager(new LinearLayoutManager(LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
StoreRecyclerViewAdapter adapter = new StoreRecyclerViewAdapter(this, allPlayers, allPrices, allStatus, open, close);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();

Upvotes: 0

Sumit Shukla
Sumit Shukla

Reputation: 4494

Call below method to refresh inside adapter:

notifyDataSetChanged()

Inside activity

if (recyclerView.getAdapter() != null) {
     recyclerView.getAdapter().notifyDataSetChanged();
}

Upvotes: 1

Android Geek
Android Geek

Reputation: 9225

if you have used recyclerview and want to change whole recycler view item so you can use notifyDataSetChanged(), if you delete some item from recyclerview so you can use notifyItemDeleted() and if you have added some more data so after add data just call notifyItemChanged(), notifyItemInserted()

if you want to add delete and update data from activity so you can use interface

Upvotes: 1

Alex
Alex

Reputation: 972

To change only one item on the list when you click it, after changing it's parameters, call:

notifyItemChanged(position) 

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93552

If you want the whole set of items to change, just call notifyDataSetChanged() within the adapter. If you know which items changed, notifyItemChanged(), notifyItemInserted() and notifyItemDeleted() are more efficient.

Upvotes: 1

Related Questions