我在Recycler View中使用了checkBox,当选中checkBox时,ArrayList收藏夹根据位置从ArrayList list_items填充新的Item,当点击收藏夹按钮时,Recycler View填充了ArrayList收藏夹,但选中的复选框已经消失了,当选择onBackPressed时,回收器视图显示为填充了没有选中复选框的ArrayList list_items,我尝试使用共享首选项来解决此问题,并且如果我关闭应用程序但未成功则保持复选框选择
class RecyclerView_dAdapter extends RecyclerView.Adapter {
private List<List_Item> list_items;
private ArrayList favorite = new ArrayList();
private List<List_Item> favorite_itemes = new ArrayList<>();
private Context context;
RecyclerView_dAdapter(List<List_Item> list_Item, Context context) {
list_items = list_Item;
this.context = context;
}
@Override
public RecyclerView_dAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final RecyclerView_dAdapter.ViewHolder holder, final int position) {
final List_Item current = list_items.get(position);
final String name = current.getName();
holder.TextName.setText(name);
holder.TextName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Show_Team_Activity.class);
intent.putExtra("Team_num", name);
context.startActivity(intent);
}
});
holder.favorite_checkBox.setChecked(favorite_itemes.contains(current));
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
CheckBox chk = (CheckBox) v;
if (chk.isChecked()) {
favorite_itemes.add(list_items.get(pos));
chk.setChecked(true);
} else if (!chk.isChecked()) {
favorite_itemes.remove(list_items.get(pos));
chk.setChecked(false);
}
}
});
}
ArrayList favorite_array(){
for (int i = 0; i < favorite_itemes.size(); i++){
favorite.add(favorite_itemes.get(i));
}return favorite;
}
@Override
public int getItemCount() {
return list_items.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView TextName;
private CheckBox favorite_checkBox;
ItemClickListener itemClickListener;
ViewHolder(View view) {
super(view);
TextName = (TextView) view.findViewById(R.id.textView_rowitem);
favorite_checkBox = (CheckBox) view.findViewById(R.id.favorite_checkBox);
favorite_checkBox.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener ic) {
this.itemClickListener = ic;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v, getLayoutPosition());
}
}
}
答案 0 :(得分:0)
使用以下步骤: 1.将您的favourite_items转换为JSONArray并将其转换为字符串
将字符串存储在SharedPreferences
在on create中检索共享首选项,将其转换回JSON数组,然后转换为ArrayList
遍历如果ArrayList包含当前复选框,请将其标记为已选中。
答案 1 :(得分:0)
在
RecyclerAdpater
中管理两个列表在某种程度上是一种不好的方法
无论如何,您可以张贴您的List_Item
课程吗?
在不知道您的数据模型结构的情况下,我们将无法提供任何建议,但是我们仍然可以期待它的外观:
List_Item
具有ID,则可以将其存储到收藏夹 SharedPreferences
中,并在以后检查它是否包含此ID。