开发者

ListPreference no selection. How to?

开发者 https://www.devze.com 2023-03-24 12:07 出处:网络
I have a ListPreference and want to verify if there is no selection to make some c开发者_运维知识库ode/treatement. How can i do this ?

I have a ListPreference and want to verify if there is no selection to make some c开发者_运维知识库ode/treatement. How can i do this ? I have this to verify the selection:

if (Integer.valueOf(choice) == 0) {

What code to verify if not selection? Thank you for your help.


If there is a preference selected for this ListPreference, then it will be saved in your SharedPreferences. You can test against this value by doing something like this:

private Boolean prefHasSelection(String prefId){
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    String yourPref = sp.getString(prefId, null);
    return (yourPref != null);
}

prefHasSelection("yourPrefId"); // returns true if something is set

You could call this method at any point in your application lifecycle to determine if the preference has been set.

0

精彩评论

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