Stupe
Stupe

Reputation: 305

SharedPreferences dont save

my problem: When I want to save a sharedPreference and restart the app, it doesnt load the right value.. My code:

int punkteint = 0;
TextView test1;
String points;
private static String SHARED_PREF_ID = "0";

public void onCreate(Bundle savedInstanceState) {
    test1 = (TextView) findViewById(R.id.test1);
    SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, 0);
    points = load.getString("punkte", "0");
    points = SHARED_PREF_ID;
    test1.setText(points);
    mehrPunkte();
    }


public void mehrPunkte() {

    punkteint++;

    SHARED_PREF_ID = Integer.toString(punkteint);
    SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);
    points = load.getString("punkte", "0");
    points = SHARED_PREF_ID;
    test1.setText(points);

    SharedPreferences save = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);
    save.edit().putString("punkte", SHARED_PREF_ID).commit();
}

What did I do wrong here? Hope you can help me

Upvotes: 0

Views: 264

Answers (4)

Edward Falk
Edward Falk

Reputation: 10063

I think you've gotten yourself all twisted up here.

  private static String SHARED_PREF_ID = "0";
  ...
  SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, 0);
  points = load.getString("punkte", "0");
  points = SHARED_PREF_ID;

These lines read shared preferences from a file named "0". Is that what you wanted? That's a not-very-useful name for a file. The next line reads a string value named "punkte" into the variable "points" and then immediately discards that value and replaces it with "0".

punkteint++;
SHARED_PREF_ID = Integer.toString(punkteint);
SharedPreferences load = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);

These lines increment a counter, use it to generate a whole new filename, and loads a whole new set of preferences from that file. Again, you load "points" from those preferences and immediately discard the value, setting it to this new filename.

SharedPreferences save = getSharedPreferences(SHARED_PREF_ID, MODE_PRIVATE);
save.edit().putString("punkte", SHARED_PREF_ID).commit();

These lines re-load the preferences from that new file again and writes the filename into the "punkte" value.

It looks like you're deliberately generating a series of preferences files, each of which contains its own name under the value "punkte".

When you next run the app, you'll go back to reading preferences from file "0", getting the same value you got the first time: "0"

It would help if you told us what you were actually trying to do.

Upvotes: 0

Kaediil
Kaediil

Reputation: 5535

This line:

save.edit().putString("punkte", SHARED_PREF_ID).commit();

is saving SHARED_PREF_ID as the value of punkte. So, it will always be 0. Maybe you mean:

save.edit().putString("punkte", String.valueOf(punkteint)).commit();

or maybe:

save.edit().putString("punkte", String.valueOf(points)).commit();

I am not sure what you are trying to actually save.

Oh and you are resettiong the value as soonas you get it from the shared prefs:

points = load.getString("punkte", "0");
points = SHARED_PREF_ID;

Don't do the second line.

Upvotes: 1

Cat
Cat

Reputation: 67502

Not sure what you're trying to do here:

points = load.getString("punkte", "0");
points = SHARED_PREF_ID;
test1.setText(points);

Basically, what you've told it to do is "load punkte's value into points, then load SHARED_PREF_ID into points". That means points will always be equal to SHARED_PREF_ID here. Remove the lines that say "points = SHARED_PREF_ID;" and you will probably have more success.

Additionally, I would not change SHARED_PREF_ID's value. Make it a final String and give it some unique value (like "punkte_prefs").

Lastly, I would take a look at the documentation which has great examples, and @Kaediil's answer as well (as he caught something I missed...).

Upvotes: 1

FabianCook
FabianCook

Reputation: 20557

Not sure. What your doing should work but I would use this instead

SharedPreferences.Editor prefEditor = save.edit();
prefEditor.putString("blah", blah);
PredEditor.commit()

Do the save in onPause and load in onResume. Could help if the data is meant to change thrpught the activity.

Upvotes: 0

Related Questions