Thursday, September 30, 2010

[android-developers] Re: Setting/Passing values

I looked at the URL's that you provided and tried some coding with it,
but I am failing to understand
the proper coding methods.

In the main, I have the options menu as:

// options menu

// Called only the first time the options menu is displayed.
// Create the menu entries.
// Menu adds items in the order shown.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Setup")
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add("About")
.setIcon(android.R.drawable.ic_menu_info_details);
return(super.onCreateOptionsMenu(menu));
//return true;
}

// handle menu selected
public boolean onOptionsItemSelected(MenuItem item){

if (item.getTitle().equals("Setup")){
Intent intent = new Intent(this,
com.tccalculator.setup.class);
startActivityForResult(intent, MY_SETUP);
return true;
}

if (item.getTitle().equals("About")){
Intent intent = new Intent(this,
com.tccalculator.about.class);
startActivityForResult(intent, MY_ABOUT);
return true;
}
return(super.onOptionsItemSelected(item));
//return false;
}

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

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();
}

The PREFS_NAME was declared at the top:
public static final String PREFS_NAME = "calcsettings";

If I am following this, the "editor" creates the name/value pair
("tview1" = "this is a test") then writes it to the preferences
by calling the editor.commit();

What am I doing wrong? As I am testing, I am attempting to creating/
retrievie
one preference (name/value).

Thanks again for all your help.

:RB


On Sep 30, 4:59 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> On Thu, Sep 30, 2010 at 6:51 PM, rb <rbs...@gmail.com> wrote:
> > Thanks for shedding some light on this issue.
>
> > Here's some of my code:
>
> :: snip ::
>
> > I was going to save and read the data from a file which would be
> > stored
> > in the phones default applications directory as one file.
>
> All the more reason to use a PreferenceActivity.
>
> > Not sure how you would use the preference activity for storing name/
> > values as
> > I need to store and retrieve 6 different items.
>
> Ever looked at the Settings application in your device? That's a
> PreferenceActivity. That has well more than "6 different items".
>
> > Any chance you could reply with some code on how I could utilize the
> > preferences activity
> > from the code I have provided?
>
> http://developer.android.com/guide/topics/data/data-storage.html#prefhttp://developer.android.com/reference/android/preference/PreferenceA...http://github.com/commonsguy/cw-android/tree/master/Prefs/Dialogs/
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://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