Programmatically Creating ListPreference - But Entry List is Empty
Asked Answered
B

2

16

I'm trying to programmatically create a ListPreference, which I can do but when I select it, it's list of entries is empty. I believe I am correctly setting the setEntries() and setEntryValues() with CharSequence arrays, but it's just empty when I select it.

Please find below the ActivitySetting class. Please note I'm using PreferenceFragments as to not use deprecated methods. But I only have one PreferenceFragment which is currently set as default

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}

public static class PrefsFragment extends PreferenceFragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.settings);

        // Create the new ListPref
        ListPreference customListPref = new ListPreference(getActivity());

        // Get the Preference Category which we want to add the ListPreference to
        PreferenceCategory targetCategory = (PreferenceCategory) findPreference("TARGET_CATEGORY");

         CharSequence[] entries = new CharSequence[]{"One", "Two", "Three"};
         CharSequence[] entryValues = new CharSequence[]{ "1", "2", "3" };

         // IMPORTANT - This is where set entries...looks OK to me
         customListPref.setEntries(entries);
         customListPref.setEntryValues(entryValues);

         customListPref.setTitle("Title");
         customListPref.setSummary("This is the summary");
         customListPref.setDialogMessage("Dialog Message");
         customListPref.setPersistent(true);

         // Add the ListPref to the Pref category
         targetCategory.addPreference(customListPref);
    }
 }
}

Here is the Setting.xml it just has the single PreferenceCategory which the ListPreference is added to:

   <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="Some Options" android:key="TARGET_CATEGORY">
    </PreferenceCategory>
</PreferenceScreen>

Here is what I get. The ListPreference has been successfully but when I select it.... No entries :( I'm expecting the options: "One", "Two", "Three"

enter image description here

Bag answered 5/3, 2014 at 15:48 Comment(1)
Thanks I was have a bit of a time implementing the answer from https://mcmap.net/q/377362/-dynamic-listpreference-in-android this was not working new ListPreference(this) although getActivity did work new ListPreference(getActivity())Strick
B
10

Found it if you set the setDialogMessage() then this overwrites the contents so by removing this line, it works now.

Bag answered 5/3, 2014 at 16:26 Comment(0)
I
3

You might want to replace setDialogMessage() by setDialogTitle() to get the title back.

Inhumanity answered 26/5, 2015 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.