androidlistpreference

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.

In this settings i have a ListPreference generated dynamically with an HTTP call that receive a JSON of values.

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.

I see that i can get entry with getEntry() method from SettingsFragment:

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?

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:

<?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

EDIT

I see that from MainActivity the sharedPref object have 2 keys:

SharedPreference sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

It's contain the key entry_preference that it's the key of the ListPreference:

   <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".

I write something wrong or it's normal that the key "entry" return the same value of entry_preference?

EDIT 2 - Possible Answer

For now i save the Entry in SharedPreference so i can get it from MainActivity just by reading it. 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

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

 // 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:

   //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


Solution

  • 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

    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

     // 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:

       //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));