Reputation: 21
I wonder if any one can help me, i am trying to get my head around shared preferences, i assume they are stored in the device (tablet) and can be checked to see they exist. My code below (first one) i want a button once clicked to put a string or boolean in the shared preferences. The second code is to see if the shared prefs exist if it does make a settext change if not ignore and look for the next string
cala1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LoadPreferences();
SharedPreferences sharedpreferences = getSharedPreferences("prefman", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("cal1","c1");
editor.commit(); });
enter4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LoadPreferences();
SharedPreferences sharedpreferences = getSharedPreferences("prefman", MODE_PRIVATE);
sharedpreferences.contains("cal1");
if (sharedpreferences.getString("cal1","c1").equals("cal1"));
{
{cexist1.setText("Shared prefs exit");
}
else
Upvotes: 2
Views: 10341
Reputation: 1745
I don't get why you call sharedpreferences.contains("cal1")
when you ignore the return value anyway.
The Android documentation for SharedPreferences says the following:
contains(String key) Checks whether the preferences contains a preference.
Looks like that is what you want, try using the call in you if clause.
if (sharedpreferences.contains("cal1")) {
cexist1.setText("Shared prefs exit");
}
the format of your code above is a bit messy too - makes it harder to read ;)
Upvotes: 17