Adding IF statement into each array index value in MulitSelectList preference

390 views Asked by At

I'm trying to put an IF statement for each array index value, the value is from MultiSelectList preference. but I'm having problem on doing it, here is my code please help me.

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
}

}

this is my MultiSelectList from preference fragment

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

There are 2 answers

0
HesZrave On BEST ANSWER

try this.

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
}
3
Hala.M On

selectedDoa is an array not a single string value so you will need to check all the selected indices try this

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

as our friend Vaiden said if switch string are an issue

replace them with

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