문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top