views:

40

answers:

1

I have my menu button bringing up a Settings option, which brings up numerous ListPreferences such as weight and various sizes for glasses (it's a BAC calculator app). I'll pick one example... weight will work.

Depending on how much you weigh it will affect your BAC. I have a int for Weight, set at 180. I would like someone to be able to go into the menu Settings, pick the "Weight" ListPreference, and choose between 100, 130, 150, 180, 210, 240, 270, and 300. I already have the numbers show up (all of the arrays have been created) and I can choose one, but it doesn't do anything because it's not linked up with the int Weight variable. How do I go about linking the information?

A: 

if you use standard PreferenceActivities:

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    // <snip>

    public void onSharedPreferenceChanged(SharedPreferences pref0, String key) {
        if (key.equals("weight")){
            int weight = pref0.getInt("weight",100));

            // <you have now weight as int!>
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
zed_0xff
No dice. The weight int still isn't the weight variable in my main class.
Dan T
then implement OnSharedPreferenceChangeListener in your class and it will catch weight changes via preferences
zed_0xff
so what would I put in my class for int Weight = ?
Dan T
int Weight = 0; and then all methods from my original message. and don't forget "implements OnSharedPreferenceChangeListener"
zed_0xff