Thursday, September 30, 2010

Re: [android-developers] Re: Setting/Passing values

On Thu, Sep 30, 2010 at 9:15 PM, rb <rbsata@gmail.com> wrote:
> I believe that I need this to read the preferences still in the main
> and set a textview as such:
>
>        public void onResume() {
>                super.onResume();
>
>                SharedPreferences prefs=PreferenceManager
>                        .getDefaultSharedPreferences(this);
>                TextView tv = (TextView) findViewById(R.id.TextView01);
>                tv.setText(prefs.getString("tview1", ""));
>
> The preference name for this textview is called "tview1".  According
> to the example, it looks like you have
> to give it a value.

You have to give it a default value, in case the preference has not
yet been set by the user. That is the second parameter to getString()
("Value to return if this preference does not exist", per the
documentation):

http://developer.android.com/reference/android/content/SharedPreferences.html#getString(java.lang.String,
java.lang.String)

>  I just want to read that preference, and take the
> value it has, and place it in the TextView01 text
> but according to the example, you have to give it a value.

I repeat, you have to give it a default value, in case the preference
has not yet been set by the user.

> In the setup, I created this:
>    @Override
>    protected void onStop(){
>       super.onStop();
>
>      // We need an Editor object to make preference changes.
>      // All objects are from android.context.Context
>      SharedPreferences settings = getSharedPreferences(PREFS_NAME,
> 0);
>      SharedPreferences.Editor editor = settings.edit();
>
>      editor.putString("tview1","this is a test");
>
>      // Commit the edits!
>      editor.commit();
>    }

This should not be needed.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 3.1 Available!

--
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