mardi 25 août 2020

How can I save CheckBox state from RecyclerView?

I am using a RecyclerView and an adapter to display the data of an array of Event objects. In RecyclerView, I use a CheckBox for one of the object's parameters. How can I save it? For the rest I use SharedPreferences, but here I don't know how to apply it in my Activity.

Here's my class Event:

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;
}

}

And my Recycler View and Adapter:

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();
}

}

For example, for ItemClick and changes saving in my Activity I use 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();
}



Aucun commentaire:

Enregistrer un commentaire