Saturday, August 13, 2011

[android-developers] Re: Shared preferences and integer values

On Aug 13, 9:22 am, Kristoffer <kris.isak.v...@gmail.com> wrote:
> Hello.
>
> Iam using shared preferences to store some values.
>
> final SharedPreferences pref = getSharedPreferences("MyPref",
> MODE_PRIVATE);
>
> final SharedPreferences.Editor edit = pref.edit();
>
> edit.putInt("Int_value", 1);
> edit.putString("String_Value", "test string");
> edit.commit();
>
> i look in the xml file and everything is saves as it should.
>
> now i grabb the values:
>
> int i = pref.getInt("Int_Value", 200);
> String s = pref.getString("String_Value", "");
>
> and now i wanna add it to a text label:
>
> I have this in top of my code
>
> final TextView test = (TextView) findViewById(R.id.test);
>
> and then if i wanna show the string everything works as it should.
>
> test.setText(s);
>
> if i try to show the int value with this
>
> test.setText(i);
>
> then my application make a force close, so i thougt that the problem
> was that i had to make the int a string so i did like this
>
>  test.setText(Integer.toString(i));
>
> now the program wotk but it does not show my stored value it shows me
> the in this case 200 becourse of
>
> int i = pref.getInt("Int_Value", 200);
>
> I must be missing something simple here i quess.
> could someone point it out for me?

Change:

edit.putInt("Int_value", 1) ;

to:

edit.putInt("Int_Value", 1) ;

pskink

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment