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);
}
}
}
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Open build. gradle and add Recycler view & Card view library dependencies. Step 3 − Add the following code to res/layout/activity_main.
To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView.
ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).
Write in Adapter class in onBindViewHolder
photolist.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, photolist.size());
notifyDataSetChanged();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With