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

Adding IF statement into each array index value in MulitSelectList preference

我正在尝试为每个数组索引值放置一个 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
}

}

这是我的偏好片段中的 MultiSelectList

<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;
        }
    }

正如我们的朋友 Vaiden 所说,如果开关字符串是一个问题

将它们替换为

if(index.equals("0"){
//do what zero does
}
else if(index.equals("1"){
//do what one does
}

试试这个。

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
}