从RecyclerView中删除卡,然后再次重新添加 [英] Deleting Card from RecyclerView and re-add it again
问题描述
如果用户另行决定,我正在努力实现功能,以删除recyclerview的条目并再次重新添加.为此,我正在显示带有撤消操作的 Snackbar
.
因此流程应为:用户看到 Cards
的列表,每个列表都显示一些值和一个删除按钮.如果用户按下删除按钮,则该卡将被删除,并显示一个 Snackbar
.如果用户在 Snackbar
上单击撤消",则应将 Card
重新添加到 RecyclerView
中.仅当 Snackbar
在超时后消失时,相应的条目也应从 SQLite
数据库中删除.
我正在填充各个 Cards
的 TextViews
,并在 onBindViewHolder
OnClickListener >.
我的类 CardApater
扩展了 RecyclerView.Adapter< CardAdapter.CardViewHolder>
如下:
私有列表< CardEntry>牌;公用CardAdapter(列表< CardEntry>卡){this.cards =卡;}@Overridepublic int getItemCount(){return cards.size();}@Overridepublic void onBindViewHolder(final CardViewHolder cardViewHolder,int i){最终CardEntry卡= cards.get(i);cardViewHolder.tvDate.setText(card.date);cardViewHolder.tvValue1.setText(card.value1);cardViewHolder.tvValue2.setText(card.value2);cardViewHolder.tvValue3.setText(card.value3);cardViewHolder.deleteButton.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){cards.remove(cardViewHolder.getAdapterPosition());notifyDataSetChanged();小吃店小吃= Snackbar.make(view,"Deleted",Snackbar.LENGTH_LONG);dessert.setAction("Undo",this);快餐.setActionTextColor(Color.RED);快餐.setCallback(新Snackbar.Callback(){@Override公共无效onDismissed(小吃店小吃店,国际事件){super.onDismissed(小吃吧,事件);切换(事件){情况DISMISS_EVENT_ACTION:cards.add(card);notifyDataSetChanged();休息;情况DISMISS_EVENT_TIMEOUT:MainActivity.datasource.deleteSQLiteEntry(card.id);休息;}}});快餐.表演();}});}@Override公共CardViewHolder onCreateViewHolder(ViewGroup viewGroup,int i){查看itemView = LayoutInflater.来自(viewGroup.getContext()).inflate(R.layout.cardview,viewGroup,false);返回新的CardViewHolder(itemView);}公共静态类CardViewHolder扩展了RecyclerView.ViewHolder {受保护的TextView tvDate;受保护的TextView tvValue1;受保护的TextView tvValue2;受保护的TextView tvValue3;受保护的ImageView deleteButton;公共CardViewHolder(查看v){超级(v);tvDate =(TextView)v.findViewById(R.id.date);tvValue1 =(TextView)v.findViewById(R.id.value1);tvValue2 =(TextView)v.findViewById(R.id.value2);tvValue3 =(TextView)v.findViewById(R.id.value3);deleteButton =(ImageView)v.findViewById(R.id.delete);}}
删除卡是可行的,但是当将CardEntry重新添加到列表中时,应用程序崩溃,原因是 ArrayIndexOutOfBoundsException:length = 12;在
. onClick
方法中的 cards.remove(cardViewHolder.getAdapterPosition());
行处,index = -1
任何有关添加新条目时为何调用 onClick
的建议,都将受到赞赏.
这可能是由于回收.当前,已卸下的卡的适配器位置没有指向任何东西.您应该将该位置放在onBindViewHolder的范围之外,并使用该位置重新添加它.
I'm struggling to implement the funtionality to delete an entry of a recyclerview and re-add it again, if the user decided otherwise. For that I'm showing a Snackbar
with an undo action.
So the flow should be: The user sees a list of Cards
which each show some values and an delete button. If the user presses the delete button, the card is deleted and a Snackbar
is shown. If the user clicks undo on the Snackbar
the Card
should be re-added to the RecyclerView
. Only if the Snackbar
disappears by timeout, the respective entry should also be deleted from SQLite
database.
I'm populating the TextViews
of the respective Cards
and set the OnClickListener
of the delete button in onBindViewHolder
.
My class CardApater
extending RecyclerView.Adapter<CardAdapter.CardViewHolder>
looks like the follwing:
private List<CardEntry> cards;
public CardAdapter(List<CardEntry> cards) {
this.cards = cards;
}
@Override
public int getItemCount() {
return cards.size();
}
@Override
public void onBindViewHolder(final CardViewHolder cardViewHolder, int i) {
final CardEntry card = cards.get(i);
cardViewHolder.tvDate.setText(card.date);
cardViewHolder.tvValue1.setText(card.value1);
cardViewHolder.tvValue2.setText(card.value2);
cardViewHolder.tvValue3.setText(card.value3);
cardViewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cards.remove(cardViewHolder.getAdapterPosition());
notifyDataSetChanged();
Snackbar snack = Snackbar.make(view, "Deleted", Snackbar.LENGTH_LONG);
snack.setAction("Undo", this);
snack.setActionTextColor(Color.RED);
snack.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
switch (event) {
case DISMISS_EVENT_ACTION:
cards.add(card);
notifyDataSetChanged();
break;
case DISMISS_EVENT_TIMEOUT:
MainActivity.datasource.deleteSQLiteEntry(card.id);
break;
}
}
});
snack.show();
}
});
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardview, viewGroup, false);
return new CardViewHolder(itemView);
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
protected TextView tvDate;
protected TextView tvValue1;
protected TextView tvValue2;
protected TextView tvValue3;
protected ImageView deleteButton;
public CardViewHolder(View v) {
super(v);
tvDate = (TextView) v.findViewById(R.id.date);
tvValue1 = (TextView) v.findViewById(R.id.value1);
tvValue2 = (TextView) v.findViewById(R.id.value2);
tvValue3 = (TextView) v.findViewById(R.id.value3);
deleteButton = (ImageView) v.findViewById(R.id.delete);
}
}
Deleting the card works, but when re-adding the CardEntry to the list, the app crashes with ArrayIndexOutOfBoundsException: length=12; index=-1
at the line cards.remove(cardViewHolder.getAdapterPosition());
in the onClick
method.
Any suggestions on that or why onClick
is called at all when adding a new entry are appreciated.
This might be because of recycling. The adapter position of the removed card, currently points to nothing. You should keep the position out of the scope of the onBindViewHolder and use that one to re-add it.
这篇关于从RecyclerView中删除卡,然后再次重新添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!