Reputation: 35
I'm trying to save the selections of checkboxes when I open the app or go to a different activity, it is a simple program that shows a list of countries and flags that you can set as favorites, I just cannot see where I've gone wrong here:
public class MyAdapter extends ArrayAdapter<String> {
String[] names;
int[] flags;
Context mContext;
public MyAdapter(Context context, String[] countryNames, int[] countryFlags, String[] countryDetails) {
super(context, R.layout.listview_item);
this.names = countryNames;
this.flags = countryFlags;
this.mContext = context;
}
@Override
public int getCount() {
return names.length;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
boolean shouldBeChecked = sp.getBoolean(names[position], false);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
mViewHolder.mCheckBox = convertView.findViewById(R.id.check_Box);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mContext.getSharedPreferences("FreshStart",0);
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);
mViewHolder.mCheckBox.setTag(names[position]);
convertView.setTag(mViewHolder);
mViewHolder.mCheckBox.setChecked(shouldBeChecked);
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setSelected(true);
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
}
else{
buttonView.setSelected(false);
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mName;
CheckBox mCheckBox;
}
}
Its the if(isChecked)
part where I seem to be struggling the most right now, I know that I need to save the instance state and I've attempted to use the SharedPreferences
to save but not completely sure how to use it and was hoping for some help in doing so to help understand it some more
Upvotes: 0
Views: 90
Reputation: 262
public class MyAdapter extends ArrayAdapter<String> {
String[] names;
int[] flags;
Context mContext;
public MyAdapter(Context context, String[] countryNames, int[] countryFlags, String[] countryDetails) {
super(context, R.layout.listview_item);
this.names = countryNames;
this.flags = countryFlags;
this.mContext = context;
}
@Override
public int getCount() {
return names.length;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
final SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
boolean shouldBeChecked = sp.getBoolean(names[position], false);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
mViewHolder.mCheckBox = convertView.findViewById(R.id.check_Box);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mContext.getSharedPreferences("FreshStart",0);
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);
mViewHolder.mCheckBox.setTag(names[position]);
convertView.setTag(mViewHolder);
mViewHolder.mCheckBox.setChecked(shouldBeChecked);
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor preferencesEditor = sp.edit();
preferencesEditor.putBoolean("FreshStart", isChecked);
preferencesEditor.apply();
buttonView.setSelected(isChecked);
if (isChecked)
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mName;
CheckBox mCheckBox;
}
}
Upvotes: 1
Reputation: 8119
mViewHolder.mCheckBox.setChecked(getSharedBoolean(context,"my_switch_key",false));
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView.setSelected(true); // No need to setSelected as callback fired after button check status already change.
storeSharedItem(context,"storeSharedItem",isChecked);
}
}
});
/*
* RCHIVED_SHARED_PREF "String" the Desired preferences file. If a preferences file by this name
* does not exist, it will be created when you retrieve an editor
*/
private final static ARCHIVED_SHARED_PREF = "shared_pref"; // use any String to define SharedPreference storage.
public static boolean getSharedBoolean(Context ctx, String key,boolean def) {
return ctx.getSharedPreferences(ARCHIVED_SHARED_PREF, 0).getBoolean(key, def);
}
public static void storeSharedItem(Context ctx, String key, boolean value) {
SharedPreferences sharedPref = ctx.getSharedPreferences(ARCHIVED_SHARED_PREF, 0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(key, value);
editor.apply();
}
Upvotes: 0
Reputation: 248
Do it this way:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); //mContext is your context
final SharedPreferences.Editor editor = prefs.edit();
buttonView.setSelected(true);
if (isChecked){
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
editor.putBoolean("some_name",true); //
editor.apply();
}
else{
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
editor. editor.putBoolean("some_name",false);
editor.apply();
}
}
Hope that helps.
Upvotes: 1