Android: ListPreference hide/disable value
I'm generating a listPreference dialog based on xml (entries and entryValues).
<string-array name="listArray">
<item>Title (A-Z)</item>
<item>Title (Z-A)</item>
<item>Visits (default)</item>
<item>Date</item>
<item>Manual</item>
</string-array>
<string-array name="listValues">
<item>titleASC</item>
<item>titleDESC</item>
<item>visitsSort</item>
<item>createSort</item>
<item>manualSort</item>
</string-array>
I want to hide/disable some of the entries (for example: Manual) based on some other parame开发者_Python百科ters.
I understand it should be inside this scope:
Preference sorted = (Preference) findPreference(OPT_SORT);
sorted.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (params) {
What should i put here to hide/disable some of the entries?
}
return true;
}
});
Thank you!
EDIT:
Best solution I found is to load a different set of Entries and EntryValues. On Preference class (onCreate):
ListPreference sortBy = (ListPreference) findPreference(OPT_SORT);
if (isTabletDevice()) {
sortBy.setEntries(getResources().getStringArray(R.array.HClistArray));
sortBy.setEntryValues(getResources().getStringArray(R.array.HClistValues));
}
Hope this helps anyone! :)
AFAIK, ListPreference
does not support this. You might be able to create your own subclass of ListPreference
where you use a custom Adapter
and indicate which items are and are not enabled.
It's quite late answer, I hope that's will help.
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
// If it's sync_factory_list, enable/disable appropriate settings
if(preference.getKey().equalsIgnoreCase("sync_factory_list")){
Preference p_api_key = preference.getPreferenceManager().findPreference("ws_api_key");
Preference p_api_url = preference.getPreferenceManager().findPreference("ws_api_url");
switch ( index) {
case 0: //local
p_api_key.setEnabled(false);
p_api_url.setEnabled(false);
break;
case 1: //prestashop webservice
p_api_key.setEnabled(true);
p_api_url.setEnabled(true);
break;
case 2: //thrift
p_api_key.setEnabled(false);
p_api_url.setEnabled(false);
break;
default:
p_api_key.setEnabled(false);
p_api_url.setEnabled(false);
break;
}
}
}
...
return true;
}
}
精彩评论