简体   繁体   中英

MultiSelectListPreference setEntries doesn't show up

I am trying to build an android app and I need a MultiSelectListPreference option in the settings menu. I have created a PreferenceActivity to handle this and I created a preferences.xml file as well, but I need to be able to load the list elements dynamically in the program. I know that I need to use the setEntries and setEntryValues methods to do this, but when I use these methods no exceptions are thrown and the title and summary of the MultiSelectListPreferenc show up but no elements appear.

I have verified that the arrays I am using to populate entries and entryValues are not empty by printing them out, as well as by printing out the result of getEntries() and getEntryValues() after having set them and both these show the entry list to be populated; however no elements show up.

My preferences.xml code:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <MultiSelectListPreference
        android:key="markedApps"
        android:title="Targeted Apps"
        android:summary="Select apps to conditionally disable" />
</PreferenceScreen>

My AppSettings.java code:

public class AppSettings extends PreferenceActivity {
    public static MultiSelectListPreference blocked;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        blocked = new MultiSelectListPreference(this);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefFrag()).commit();
    }
    public static class PrefFrag extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            MultiSelectListPreference blocked = (MultiSelectListPreference)findPreference("markedApps");
            if (blocked == null)
                Log.e("NullPointerException", "Blocked is null");
            AppSelector.populateAppList();
            CharSequence[] appNames = new CharSequence[AppSelector.Data.appNames.size()];
            CharSequence[] allApps = new CharSequence[AppSelector.Data.allApps.size()];
            int i = 0;
            for (String appName : AppSelector.Data.appNames)
                appNames[i++] = (CharSequence) appName;
            i = 0;
            for (String app : AppSelector.Data.allApps)
                allApps[i++] = (CharSequence) app;
            blocked.setEntries(appNames);
            blocked.setEntryValues(allApps);

        }
    }
}

Thank you in advance for any help you provide.

Wow so I feel extremely stupid now. Turns out my problem was that I expected the list to show up under the title and summary whereas you actually have to click on the title and summary to make the list pop up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM