我已经在recyclerView中列出了卡片列表。当长按卡片时,相应的数据库条目将被删除,因此之后,必须删除该卡片。可以通过调用设置新的更新适配器来完成此操作。但是我无法在onBindViewHolder中使用setAdapter函数,因为我不知道如何获取相同的recyclerView。请帮忙。
代码:
代码:
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);
}
}
}