I searched through and across the network, and could not find a way to achieve this. The answer above did not help me. I found the whole ArrayAdapter method very unintuitive, useless, and difficult to implement.
Finally, I really had to look into the source code for the โListPreferenceโ, see what they did there, and figure out how to undo the default behavior cleanly and efficiently.
I share my decision below. I made the "SelectiveListPreference" class to inherit the "ListPreference" behavior, but added a positive button and did not close when the option was clicked. There is also a new xml attribute to indicate which options are available in the free version.
My trick is not to invoke the ListPreference version for onPrepareDialogBuilder, but instead implement your own using a special click handler. I did not have to write my own code to save the selected value, since I used the ListPreference code (so I expanded the "ListPreference" rather than the "Preference").
The handler looks for the logical resource "free_version", and if it is true, it only allows the parameters specified in the xml_ entry_values_free attribute. If "free_version" is false, all options are allowed. There is also an empty method for inheritors if something should happen when choosing an option.
Enjoy,
Tal
public class SelectiveListPreference extends ListPreference { private int mSelectedIndex; private Collection<CharSequence> mEntryValuesFree; private Boolean mFreeVersion; public SelectiveListPreference(Context context) { super(context); }
EDIT: we also need to override the implemented onDialogClosed from the ListPreference (and do nothing), otherwise the values โโare not saved. Add:
protected void onDialogClosed(boolean positiveResult) {}
Tall
source share