简体   繁体   中英

How to add ListPreference in this Activity?

I am a newbie at android development and I am trying to implement a Settings Activity so that it can take user preferences. This is my Settings Activity which already includes code to handle Edit Preference changes.The module is implemented for Android 3.0 and above hence Settings Fragment is used.

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}

public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.pref_general);
    }

    @Override
    public void onResume(){
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause(){
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key){
        if(key.equals("location")){
            Preference loc_preference=findPreference("location");
            loc_preference.setSummary(sharedPreferences.getString(key,""));
        }
    }
}

}

This is the pref_gen.xml file

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<EditTextPreference
    android:key="@string/pref_loc_key"
    android:dialogMessage="@string/location_dialog_message"
    android:dialogTitle="@string/location_dialog"
    android:title="@string/pref_loc_label"
    android:defaultValue="@string/pref_default_loc"
    android:selectAllOnFocus="true"
    android:singleLine="true" />

<ListPreference
    android:key="@string/pref_unit_key"
    android:dialogTitle="@string/unit_dialog_message"
    android:title="@string/pref_unit_label"
    android:defaultValue="@string/pref_default_unit"
    android:entries="@array/pref_unit_options"
    android:entryValues="@array/pref_unit_values" />

This is the arrays.xml file

<string-array name="pref_unit_options">
    <item>Metric</item>
    <item>Imperial</item>
</string-array>
<string-array name="pref_unit_values">
    <item>1</item>
    <item>0</item>
</string-array>

Can anyone help me to add the List Preference module in the Settings Fragment method along with a listener? I have gone through other posts but somehow I cant modify it accordingly.

You can use getPreference to reference ListPreference in java and then register listener on it.

ListPreference unit_preference = mContext.getPreference(<Key>);

unit_preference .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
     public boolean onPreferenceChanged(Preference preference, Object newValue) {
          //Do something with newValue here
    }
});

Note: mContext is a class variable of type Context . You must set it before calling beginTransaction :

mContext = this;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM