java - 动态添加选项到列表首选项

标签 java android listpreference

我正在编写一个具有 PreferenceActivity 和多个 PreferenceFragment 的程序。用户选择的选项之一是要连接的服务器,这是从 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>

这显然工作得很好,你会在列表中得到三个名字。然而,有一个单独的 fragment 允许用户输入名称和 IP 地址,然后将其作为额外选项添加到下拉列表中。

我有一个可行的解决方案,涉及加载外部文件、清除条目并从文件中添加条目。这是“好的”,但我想使用 sharedpreferences 来保存这些额外的值。我的问题是,如何使用编辑器编写每次启动应用程序时保存的额外选项?

我已经考虑过使用 EditorputStringSetcommit,但添加的选项没有出现在下拉列表中。有一些相关的帖子似乎处理 TextPreference 但这些解决方案并没有解决我的问题。

编辑,这就是我创建 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" />

我在标签上有一个点击处理程序以添加到 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;
            }
        });

最佳答案

写入共享首选项

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

阅读自:

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

这就是你要问的吗?

编辑:

看看你的代码,你似乎错过了实际的保存。

检查 Gregor 的答案: How to add new value to listpreference and save it?

他基本上说每次打开对话框时,都会加载 xml 中的列表。您需要更改行为以包含 SharedPreferences 中的首选项。

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

关于java - 动态添加选项到列表首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40485763/

相关文章:

java - 处理数据存储最终一致性的技术

java - 从网站获取当前时间

java - Eclipse 编辑器插件 : "ERROR" when opening file outside project

java - 来自 Android 的 Yelp v2 API 使用 SignPost oauth libs 说签名无效

android - 如何观察协程流的 ContentProvider 变化

java - 如何在一行中列出号码和联系人

android - 如何在 ListPreference 上设置 OnTouchListener()?

Android首选项 Activity 字体预览

android - checkboxpreference android 或 listpreference 的依赖中的多重依赖

java - 需要 HTTP 连接解释