I have written the following "setter" in setOnCheckedChangeListener
to set a key, value pair to sharedpreferences. However, where (in the code) should I get back the values and how should I set the checkboxes in the Recyclerview? I am very confused, so clarity would be appreciated. Thanks. "numbers" is an arraylist that holds two textviews that I set using the bindData function that you see in the code. So, numbers.get(position) sets the right values from the array to my correct cardview. However, I am unable to understand how to set the checkbox based on the key, value pair I set below.
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.bindData(numbers.get(position));
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(numbers.get(position).isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
//Log.e(Integer.toString(holder.getAdapterPosition()), " IS CHECKED");
if (isChecked) {
setDefaults(Integer.toString(holder.getAdapterPosition()), "1", context);
Boolean man = numbers.get(holder.getAdapterPosition()).isSelected();
Log.e("Setting " + Integer.toString(holder.getAdapterPosition()), man.toString());
}
}
});
}
public static void setDefaults(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.clear();
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
Please let me know if you have further questions. Regards
Aucun commentaire:
Enregistrer un commentaire