Swapnil Sharma Swapnil Sharma - 5 months ago 179
Android Question

How do I update recyclerView inside onBindViewHolder of its adapter?

I've made a list of cards inside recyclerView. When card is held for sometime, its corresponding entry in database is deleted so after that, the card must be removed. It can be done by calling setting new updated adapter. But I'm unable to use setAdapter function inside onBindViewHolder because I don't know how to get same recyclerView. Please help.

CODE:

public class RecyclerViewAdapterMain extends RecyclerView.Adapter<RecyclerViewAdapterMain.ViewHolder> {

String[] strings;
DBManager dbManager;

public RecyclerViewAdapterMain(String[] arrayList) {
this.strings = arrayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_list,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.textView.setText(strings[position]);
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(holder.itemView.getContext(),SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("list",strings[position]);
intent.putExtras(bundle);
holder.itemView.getContext().startActivity(intent);
//overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
}
});
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View view) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
dbManager = new DBManager(holder.itemView.getContext(),null,null,1);
dbManager.deleteList(strings[position]);
String[] strings = dbManager.showLists();
RecyclerViewAdapterMain recyclerViewAdapter = new RecyclerViewAdapterMain(strings);
//I WANT TO SET UPDATED ADAPTER HERE
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext());
builder.setMessage("Are you sure that you want to delete \""+strings[position]+"\"?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
return true;
}
});


}

@Override
public int getItemCount() {
return strings.length;
}

public class ViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
cardView = (CardView) itemView.findViewById(R.id.cardView);
}
}


}

Answer

You need to update your existing variable

String[] strings;

And then tell the RecyclerView to redraw the items.

notifyItemRemoved(position);

or

notifySetDataChanged();

You then get

case DialogInterface.BUTTON_POSITIVE:
                        dbManager = new DBManager(holder.itemView.getContext(),null,null,1);
                        dbManager.deleteList(strings[position]);
                        strings = dbManager.showLists();
                        notifyItemRemoved(position);
                        break;