简体   繁体   English

Android ListPreference条目文本,而不是不扩展PreferenceFragment的Activity或Fragment中的条目值

[英]Android ListPreference entry text instead of entry value from Activity or Fragment that doesn't extend PreferenceFragment

In my app i have a SettingsFragment that extends PreferenceFragmentCompat . 在我的应用程序中,我有一个SettingsFragment extends PreferenceFragmentCompat

In this settings i have a ListPreference generated dynamically with an HTTP call that receive a JSON of values. 在此设置中,我具有一个使用HTTP调用动态生成的ListPreference ,该调用接收值的JSON。

Now the problem is that from MainActivity when i get value stored into SharedPreference i get the android.entries but i didn't find a way to get the entryValues. 现在的问题是,当我将值存储到SharedPreference中时,从MainActivity获得了android.entries,但是我没有找到获取entryValues的方法。

I see that i can get entry with getEntry() method from SettingsFragment : 我看到我可以从SettingsFragment获得getEntry()方法的条目:

ListPreference listPreference = (ListPreference)
findPreference("entry_preference");
String entry = listPreference.getEntry();

But this only works in the SettingsFragment.... how can i get the selected entry instead of entryValues from MainActivity? 但这仅在SettingsFragment中起作用。...我如何从MainActivity获取所选条目而不是entryValues?

sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//This will return the value, but i need the entry text
String entry_settings = sharedPref.getString("entry_preference", "-1");

That code will return -1, but i need No Entry: 该代码将返回-1,但我不需要输入:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- dynamically generated by reading values from server -->
    <string-array name="entry_names_array">
        <item>No Entry</item>
    </string-array>

    <!-- dynamically generated by reading values from server -->
    <string-array name="entry_values_array">
        <item>-1</item>
    </string-array>
</resources>

How can i get this value into MainActivity , or more generally how get this value into Activity/Fragment that doens't extend PreferenceFragmentCompat or PrefereceActivity 我如何才能将此值获取到MainActivity ,或更一般地说,如何将该值获取到不会扩展PreferenceFragmentCompatPrefereceActivity Activity / Fragment中

EDIT 编辑

I see that from MainActivity the sharedPref object have 2 keys: 我从MainActivity看到sharedPref对象有2个键:

SharedPreference sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

It's contain the key entry_preference that it's the key of the ListPreference: 它包含键entry_preference ,它是entry_preference的键:

   <ListPreference
        android:key="entry_preference"
        android:entries="@array/entry_names_array"
        android:entryValues="@array/entry_values_array"
        android:summary="%s"
        android:title="@string/listpref_entry_settings_title" />

And another key "entry", but this key return the same value of "entry_preference". 和另一个键“ entry”,但是此键返回相同的“ entry_preference”值。

I write something wrong or it's normal that the key "entry" return the same value of entry_preference? 我写错了什么,或者键“ entry”返回相同的entry_preference值是正常的吗?

EDIT 2 - Possible Answer 编辑2-可能的答案

For now i save the Entry in SharedPreference so i can get it from MainActivity just by reading it. 现在,我将Entry保存在SharedPreference中,这样我就可以通过读取MainActivity来获取它。 I don't know if it's a good way to do that, but here the code. 我不知道这是否是一个好方法,但是这里有代码。

1) With a OnSharedPreferenceChangeListener check when user select another choice and store it to SharedPreference 1)使用OnSharedPreferenceChangeListener检查用户选择另一个选项并将其存储到SharedPreference

public class SettingsFragment extends PreferenceFragmentCompat  {
    private static final String TAG = "SettingsFragment";
    private SharedPreferences sharedPref;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        setEntracesUrl();
        requestForEntrancesToServer();

        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                Log.i(TAG, "Settings key changed: " + key);
                ListPreference listPreferenceEntrances = (ListPreference) findPreference("entry_preference");
                String currentEntry = (String) listPreferenceEntrances.getEntry();
                listPreferenceEntrances.setSummary(currentEntry);
                //Write the currentEntry into SharedPreferences
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
                editor.commit();
            }
        };
        sharedPref.registerOnSharedPreferenceChangeListener(prefListener);
    }
}

2) I receive from http request the value that create the ListPreference, so i need to take care about entries, entriesValues and summary 2)我从http请求接收创建ListPreference的值,因此我需要注意entry,entriesValues和summary

 // Code inside method evaluateJSONObject() that evaluate JSON received 
 //from http request to server that return the values for ListPreference
 String currentEntry = (String) listPreferenceEntrances.getEntry();
 listPreferenceEntrances.setEntries(entries);
 listPreferenceEntrances.setEntryValues(entryValues);
 listPreferenceEntrances.setSummary(currentEntry);
 //Write the currentEntry into SharedPreferences
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
 editor.commit();

3) Finally, in the onCreate of my MainActivity i read sharedPreferences and set the Textview with the entry text: 3)最后,在我的MainActivity的onCreate中,我读取了sharedPreferences并使用输入文本设置了Textview:

   //SharedPreference instance
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    setBaseUrl();

    // This show the string entry VALUE of selected ListPreference
    String entry_settings_id = sharedPref
            .getString("entry_preference", "-1");
    // This show the string entry of selected ListPreference
    String entry_settings_name = sharedPref
            .getString(
                    getString(R.string.current_listpreference_entry_key),
                    getString(R.string.current_listpreference_entry_default_value));

Let me know if there is a better way to do what i need! 让我知道是否有更好的方法来做我需要的事情! Thanks 谢谢

Until there isn't a better way to do that i will post my answare maybe someone else can us it. 除非有更好的方法,否则我将发布我的软件,也许其他人可以使用它。

1) With a OnSharedPreferenceChangeListener check when user select another choice and store it to SharedPreference 1)使用OnSharedPreferenceChangeListener检查用户选择另一个选项并将其存储到SharedPreference时

public class SettingsFragment extends PreferenceFragmentCompat  {
    private static final String TAG = "SettingsFragment";
    private SharedPreferences sharedPref;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        setEntracesUrl();
        requestForEntrancesToServer();

        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                Log.i(TAG, "Settings key changed: " + key);
               //Use a switch or else case that avoid loop call of OnSharedPreferenceChangeListner()
                switch(key) {
                    case "entry_preference":
                        ListPreference listPreferenceEntrances = (ListPreference) findPreference("entry_preference");
                        String currentEntry = (String) listPreferenceEntrances.getEntry();
                        listPreferenceEntrances.setSummary(currentEntry);
                        //Write the currentEntry into SharedPreferences
                        SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
                        editor.commit();
                    break;
               default:
                    break;
              }
            }
        };
        sharedPref.registerOnSharedPreferenceChangeListener(prefListener);
    }
}

2) I receive from http request the value that create the ListPreference, so i need to take care about entries, entriesValues and summary 2)我从http请求接收创建ListPreference的值,因此我需要注意entry,entriesValues和summary

 // Code inside method evaluateJSONObject() that evaluate JSON received 
 //from http request to server that return the values for ListPreference
 String currentEntry = (String) listPreferenceEntrances.getEntry();
 listPreferenceEntrances.setEntries(entries);
 listPreferenceEntrances.setEntryValues(entryValues);
 listPreferenceEntrances.setSummary(currentEntry);
 //Write the currentEntry into SharedPreferences
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
 editor.commit();

3) Finally, in the onCreate of my MainActivity i read sharedPreferences and set the Textview with the entry text: 3)最后,在我的MainActivity的onCreate中,我读取了sharedPreferences并使用输入文本设置了Textview:

   //SharedPreference instance
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    setBaseUrl();

    // This show the string entry VALUE of selected ListPreference
    String entry_settings_id = sharedPref
            .getString("entry_preference", "-1");
    // This show the string entry of selected ListPreference
    String entry_settings_name = sharedPref
            .getString(
                    getString(R.string.current_listpreference_entry_key),
                    getString(R.string.current_listpreference_entry_default_value));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改活动的ListPreference值 - android - changing ListPreference value from an Activity Android:如果数据库为空并且添加了第一项,则片段不会刷新 - Android: fragment doesn't refresh if database is empty and first entry is added ListPreference:使用字符串数组作为条目,使用整数数组作为条目值不起作用 - ListPreference: use string-array as Entry and integer-array as Entry Values doesn't work Android-ListPreference保存到文件还是在条目上单击侦听器? - Android - ListPreference save to file or on entry click listener? Android:如何从PreferenceActivity / PreferenceFragment外部打开ListPreference对话框? - Android: how to open a ListPreference dialog from outside PreferenceActivity/PreferenceFragment? 如何获取谷歌地图API从android.app.Fragment扩展而不是Fragment Activity - How to get google maps api to extend from android.app.Fragment instead of Fragment Activity Android:从PreferenceFragment关闭活动 - Android: Close activity from PreferenceFragment Android - 是否可以专门为每个 ListPreference 条目更改属性? - Android - Is it possible to change a property specifically for each ListPreference entry? ListPreference 仅在第二次选择条目时更新其值 - ListPreference updates its value only when entry is chosen for the second time Android-SharedPreferences ListPreference-PreferenceFragment onCreate未调用 - Android - SharedPreferences ListPreference - PreferenceFragment onCreate not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM