2009
09.22
09.22
Sometimes a situation arises when you want to use a certain Preference, but also want to include some business logic to map the values to different types/values depending on certain conditions. ListPreference for instance doesn’t provide any way to neatly have the entries generated dynamically or saving them to preferences other than string ones.
Reimplementing those controls isn’t very productive most of the times. There is a way to get things done quickly, but in my opinion it isn’t the cleanest of solutions. The following code shows how to re-route ListPreferences values to a boolean preference:
public class MyPreference extends ListPreference
{
// ...
@Override
protected String getPersistedString(String defaultReturnValue)
{
boolean val = super.getPersistedBoolean(defaultReturnValue.equalsIgnoreCase("true"));
return val ? "true" : "false";
}
@Override
protected boolean persistString(String value)
{
boolean val = value.equalsIgnoreCase("true");
return super.persistBoolean(val);
}
}
No Comment.
Add Your Comment