Android 将IF语句添加到MulitSelectList首选项中的每个数组索引值中

Android 将IF语句添加到MulitSelectList首选项中的每个数组索引值中,android,sharedpreferences,android-preferences,preference,multiselectlistpreference,Android,Sharedpreferences,Android Preferences,Preference,Multiselectlistpreference,我试图为每个数组索引值放置一个IF语句,该值来自MultiSelectList首选项。但是我在做这件事时遇到了问题,这是我的代码,请帮助我 public void displaySelectedDoa(View view) { mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); Set<String> selectionsDoa = mySharedPreferences.get

我试图为每个数组索引值放置一个IF语句,该值来自MultiSelectList首选项。但是我在做这件事时遇到了问题,这是我的代码,请帮助我

public void displaySelectedDoa(View view) {

mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> selectionsDoa = mySharedPreferences.getStringSet("selectedDoaKey", null);
String[] selectedDoa = selectionsDoa.toArray(new String[]{});

if (selectedDoa == "0") { // if index number = 0
    //do something at here
}
if (selectedDoa == "1") { // if index nuber = 1
    //do here
}
public void display selecteddoa(视图){
mySharedPreferences=PreferenceManager.GetDefaultSharedReferences(上下文);
Set selectionsDoa=mySharedPreferences.getStringSet(“selectedDoaKey”,null);
String[]selectedDoa=selectionsDoa.toArray(新字符串[]{});
如果(selectedDoa==“0”){//如果索引号=0
//在这里做点什么
}
if(selectedDoa==“1”){//if索引编号=1
//在这里做什么
}
}

这是我的首选项片段中的多选列表

<MultiSelectListPreference
    android:title="Select Doa"
    android:summary="select your doa"
    android:key="selectedDoaKey"
    android:entries="@array/select_doa"
    android:entryValues="@array/select_doa_value"/>

selectedDoa是一个数组,不是单个字符串值,因此您需要检查所有选定的索引 试试这个

for (int i=0;i<selectedDoa.length;i++){
        String index=selectedDoa[i];
        switch (index){
            case "0":
                //do what zero does
                continue;
            case "1":
                //do what one does
                continue;
        }
    }

selectedDoa是一个数组,不是单个字符串值,因此需要检查所有选定的索引 试试这个

for (int i=0;i<selectedDoa.length;i++){
        String index=selectedDoa[i];
        switch (index){
            case "0":
                //do what zero does
                continue;
            case "1":
                //do what one does
                continue;
        }
    }
试试这个

CharSequence[] selectedDoa = selectionsDoa.toArray(new String[]{});
    for (int i = 0; i < selectedDoa.length; i++){
        String index = selectedDoa[i].toString();
    }

 if (index.equals("string")){
//do here
}
CharSequence[]selectedDoa=selectionsDoa.toArray(新字符串[]{});
for(int i=0;i
试试这个

CharSequence[] selectedDoa = selectionsDoa.toArray(new String[]{});
    for (int i = 0; i < selectedDoa.length; i++){
        String index = selectedDoa[i].toString();
    }

 if (index.equals("string")){
//do here
}
CharSequence[]selectedDoa=selectionsDoa.toArray(新字符串[]{});
for(int i=0;i
带字符串的开关块仅在API>=19()上受支持。对于较旧的Android,要么将字符串解析为int,要么使用级联IF语句,在字符串索引=selectedDoa[I]上使用.equals();表示不兼容类型时出错。必需:java.lang.String找到:java.lang.CharSequence。如何修复?您好,选择了DOA[i];代码编译也很好,我在androidstudio mySharedPreferences.getStringSet(“selectedDoaKey”,null)上测试了它;String[]selectedDoa=selectionsDoa.toArray(新字符串[]{});对于(int i=0;i=19()上受支持。对于较旧的Android,要么将字符串解析为int,要么使用级联IF语句,在字符串索引=selectedDoa[I]上使用.equals();表示不兼容类型时出错。必需:java.lang.String找到:java.lang.CharSequence。如何修复?您好,选择了DOA[i];代码编译也很好,我在androidstudio mySharedPreferences.getStringSet(“selectedDoaKey”,null)上测试了它;String[]selectedDoa=selectionsDoa.toArray(新字符串[]{});对于(int i=0;i