我在recyclerView里面制作了一张卡片列表。当卡被保留一段时间后,其数据库中相应的条目将被删除,因此必须删除该卡。可以通过调用设置新的更新适配器来完成。但是我无法在onBindViewHolder中使用setAdapter函数,因为我不知道如何获得相同的recyclelerView。请帮忙。
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);
}
}
}
答案 0 :(得分:4)
在onBindViewHolder中的Adapter类中编写
photolist.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, photolist.size());
notifyDataSetChanged();
答案 1 :(得分:1)
您需要更新现有变量
String[] strings;
然后告诉RecyclerView重绘项目。
notifyItemRemoved(position);
或
notifyDataSetChanged();
然后你得到
case DialogInterface.BUTTON_POSITIVE:
dbManager = new DBManager(holder.itemView.getContext(),null,null,1);
dbManager.deleteList(strings[position]);
strings = dbManager.showLists();
notifyItemRemoved(position);
break;
答案 2 :(得分:0)
试试这个
RecyclerViewAdapterMain.this.notifyItemRemoved(position);