I'm just starting to use shared preferences to allow me to save the state of a checkbox when leaving the application. I've gotten it to work but then it dawned on me that maybe it would save that preference for all checkboxes unless I make another preference. Do I have to make a shared preference for each checkbox to reference?
Code right now. I know that this will check or uncheck both checkboxes depending what I leave them at because they reference the same preference.
CheckBox checkbox = (CheckBox) findViewById(R.id.description);
CheckBox checkbox1 = (CheckBox) findViewById(R.id.description1);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
boolean checked = sharedPref.getBoolean("checked", false);
checkbox.setChecked(checked);
checkbox1.setChecked(checked);
My check box handler looks like this
if(tmpChkBox.isChecked())
{
tmpChkBox.setBackgroundColor(Color.BLUE);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
sharedPref.edit().putBoolean("checked", true).commit();
}
else
{
tmpChkBox.setBackgroundColor(Color.RED);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
sharedPref.edit().putBoolean("checked", false).commit();
}
So do I need to rename the string references within one shared preference? Like the sharedPref.edit().putBoolean("checked", false).commit(); to make another one for a different check would I changed the "checked" to "checked1"?
Aucun commentaire:
Enregistrer un commentaire