如何从RecyclerView保存复选框状态



我正在使用RecyclerView和适配器来显示Event对象数组的数据。在RecyclerView中,我为对象的一个参数使用了一个复选框。如何保存?对于其余部分,我使用SharedPreferences,但在这里我不知道如何在我的"活动"中应用它。

这是我的课堂活动:

public class Event implements Serializable {
public String Name;
public Long Date; 
public String Comment;
public String Type;
public String Notify;
public Boolean IsComplete;
public Boolean IsImportant;
Event()
{
Name = "Событие";
Date = MaterialDatePicker.todayInUtcMilliseconds();
Comment = "Comment";
Type = "Material";
Notify = "Не напоминать";
IsComplete = false;
IsImportant = false;
}
Event(String name, Long date, String comment, String type, String notify, Boolean iscomplete, Boolean isimportant)
{
Name = name;
Date = date;
Comment = comment;
Type = type;
Notify = notify;
IsComplete = iscomplete;
IsImportant = isimportant;
}
public String getName() {
return Name;
}
public Long getDate() {
return Date;
}
public String getComment() {
return Comment;
}
public String getType() {
return Type;
}
public String getNotify() {
return Notify;
}
public Boolean getComplete() {
return IsComplete;
}
public Boolean getImportant() {
return IsImportant;
}

}

我的回收器视图和适配器:

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {
private static final String TAG = "myLogs";
private ArrayList<Event> mEventList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class EventViewHolder extends RecyclerView.ViewHolder{
public TextView TVtitle;
public TextView TVcomment;
public CheckBox CBimportance;

public EventViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
TVtitle = itemView.findViewById(R.id.tv1);
TVcomment = itemView.findViewById(R.id.tv2);
CBimportance = itemView.findViewById(R.id.iconImportant);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});

}
}
public EventAdapter(ArrayList<Event> listEvent){

mEventList = listEvent;}
@NonNull
@Override
public EventViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_cardview, parent, false);
EventViewHolder evh = new EventViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(@NonNull EventViewHolder holder, final int position) {
final Event item = mEventList.get(position);
holder.TVtitle.setText(item.getName());
holder.TVcomment.setText(item.getComment());
holder.CBimportance.setOnCheckedChangeListener(null);
holder.CBimportance.setChecked(item.getImportant());
holder.CBimportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
item.IsImportant = b;
if (b){
Log.d(TAG, String.valueOf(position) + " IMPORTANT");
}
else{
Log.d(TAG, String.valueOf(position) + " NON IMPORTANT");
}
}
});
}
@Override
public int getItemCount() {
return mEventList.size();
}

}

例如,对于在我的活动中保存的ItemClick和更改,我使用onItemClick:

adapterEvent.setOnItemClickListener(new EventAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
eventsProcess.remove(position);
adapterEvent.notifyItemRemoved(position);
write(getContext(), eventsProcess, PROCESSED_EVENTS);
}
});
public static void write(Context context, ArrayList<Event> events, String Key)
{
Gson gson = new Gson();
String jsonString = gson.toJson(events);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = pref.edit();
editor.putString(Key, jsonString);
editor.apply();
}

您应该能够完全按照处理"OnItemClick"的方式执行此操作。我对您的适配器进行了一些更改,添加了一个新的接口OnItemCheckedListener,然后使用此侦听器处理checkedChanged事件。(而不是像你那样只记录它(。

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {
private static final String TAG = "myLogs";
private ArrayList<Event> mEventList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
private OnItemCheckedListener mOnItemChecked;
public interface OnItemCheckedListener {
void onItemChecked (int position, boolean isImportant);
}
public void setOnItemCheckedListener (OnItemCheckedListener listener) {
mOnItemChecked = listener;
}

public static class EventViewHolder extends RecyclerView.ViewHolder{
public TextView TVtitle;
public TextView TVcomment;
public CheckBox CBimportance;



public EventViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
TVtitle = itemView.findViewById(R.id.tv1);
TVcomment = itemView.findViewById(R.id.tv2);
CBimportance = itemView.findViewById(R.id.iconImportant);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});

}
}
public EventAdapter(ArrayList<Event> listEvent){
mEventList = listEvent;
}
@NonNull
@Override
public EventViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_cardview, parent, false);
EventViewHolder evh = new EventViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(@NonNull EventViewHolder holder, final int position) {
final Event item = mEventList.get(position);
holder.TVtitle.setText(item.getName());
holder.TVcomment.setText(item.getComment());
holder.CBimportance.setOnCheckedChangeListener(null);
holder.CBimportance.setChecked(item.getImportant());
holder.CBimportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
item.IsImportant = b;
if (b) {
Log.d(TAG, String.valueOf(position) + " IMPORTANT");
}
else{
Log.d(TAG, String.valueOf(position) + " NON IMPORTANT");
}
mOnItemChecked.onItemChecked(position, b)
}
});
}
@Override
public int getItemCount() {
return mEventList.size();
}
}

现在,从您的活动中,只需设置EventAdapter的OnItemCheckedListener。

adapterEvent.setOnItemCheckedListener(new EventAdapter.OnItemCheckedListener {
@Override
public void onItemChecked (int position, boolean isImportant) {
// TODO: 
// whatever you want to do with the isImportant data 
}
});

相关内容

  • 没有找到相关文章

最新更新