簡體   English   中英

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

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

在我的應用程序中,我有一個SettingsFragment extends PreferenceFragmentCompat

在此設置中,我具有一個使用HTTP調用動態生成的ListPreference ,該調用接收值的JSON。

現在的問題是,當我將值存儲到SharedPreference中時,從MainActivity獲得了android.entries,但是我沒有找到獲取entryValues的方法。

我看到我可以從SettingsFragment獲得getEntry()方法的條目:

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

但這僅在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");

該代碼將返回-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>

我如何才能將此值獲取到MainActivity ,或更一般地說,如何將該值獲取到不會擴展PreferenceFragmentCompatPrefereceActivity Activity / Fragment中

編輯

我從MainActivity看到sharedPref對象有2個鍵:

SharedPreference sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

它包含鍵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" />

和另一個鍵“ entry”,但是此鍵返回相同的“ entry_preference”值。

我寫錯了什么,或者鍵“ entry”返回相同的entry_preference值是正常的嗎?

編輯2-可能的答案

現在,我將Entry保存在SharedPreference中,這樣我就可以通過讀取MainActivity來獲取它。 我不知道這是否是一個好方法,但是這里有代碼。

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)我從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)最后,在我的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));

讓我知道是否有更好的方法來做我需要的事情! 謝謝

除非有更好的方法,否則我將發布我的軟件,也許其他人可以使用它。

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)我從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)最后,在我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM