简体   繁体   中英

Dynamically adding options to a listpreference

I am writing a program that has a PreferenceActivity and multiple PreferenceFragment . One of the options the user selects is a server to connect to, this is populated from the XML.

<string-array name="server_names">
    <item>test</item>
    <item>item1</item>
    <item>item2</item>
</string-array>

<string-array name="server_addresses">
    <item>10.10.10.1</item> 
    <item>10.10.10.2</item>
    <item>10.10.10.3</item>
</string-array>

This obviously works fine and you get the three names in the list. However there is a separate fragment that allows the user to enter a name and ip address which is then added to the drop down as an extra option.

I have got a working solution that involved loading an external file, clearing the entries and adding the ones from the file. This is 'ok' but I want to use sharedpreferences to save these extra values. My question is this, how do I use the editor to write extra options that will be saved each time the app is launched?

I have looked at using the Editor , putStringSet and commit but the added option doesn't appear in the drop down. There are related posts that seem to deal with a TextPreference but those solutions haven't solved my problem.

edit, this is how i create my ListPreference:

    <ListPreference
      android:entries="@array/server_names"
      android:entryValues="@array/server_addresses"
      android:key="@string/countryListId"
      android:negativeButtonText="@null"
      android:positiveButtonText="@null"
      android:title="@string/pref_title_select_com_target"
      android:enabled="true"
    android:shouldDisableView="false" />

I have a click handler on a label to add to the ListPreference:

 public boolean onPreferenceClick(Preference preference) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("Add new server")
                        .setMessage("Confirm you wish to add the server?")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPref.edit();

       /*  command in here to edit the server_names and server_addresses */   
                            }})
                        .setNegativeButton(android.R.string.no, null).show();


                return true;
            }
        });

Write to shared pref

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("highscore", 5);
editor.commit();

Read from:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 0;
long highScore = sharedPref.getInt("highscore"), defaultValue);

Is this what you are asking?

EDIT:

Looking at your code, you seem to be missing the actual saving.

Check the answer from Gregor: How to add new value to listpreference and save it?

He basically says that everytime you open your dialog, the list from xml is loaded. You need to change the behaviour to include the preferences from the SharedPreferences.

You can change this behavior using the setEntries() and setEntryVaues() methods of ListPreference

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