我试图为每个数组索引值放置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"/>
试试这个
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
}
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
}