开发者

Remove Array Item from ListPreference

I have a List Preference for Accelerometer mode in my preference activity which I'm loading purely from xml. If a users device does not have or support an Accelerometer I wont to completely remove the item in the array. Do I have to create a separate list preference for this one option to remove it? (I would prefer to keep them all in one.) How can it be removed with the below setup?

Thanks for the tip.

Jason

<string-array name="mode_text">
        <item>Others Modes</item>
        <item>Others Modes</item>
        <item>Accelerometer Mode</item>
    </string-array>
    <string-array name="开发者_StackOverflowmode_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>


When you say you want to "completely remove the item in the array", do you mean you want the ListPreference to not show in the PreferenceScreen? Or you want it to return a null value when you try to get the set value?

Okay, I understand now. I think you can't remove just that item in the array. You have to set new arrays for the entries and entryValues for your ListPreference, programatically. You find the ListPreference by its key.

Here is an example:

    if (accelerometerNotSupported) {
        ListPreference accelMode = (ListPreference) findPreference("acceleratorMode");
        accelMode.setEntries(new String[]{"Others Modes","Others Modes"});
        accelMode.setEntryValues(new String[]{"0", "1"});
    }

That will change your list preference to have only those two options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜