1

각 배열 인덱스 값에 IF 문을 넣으려고합니다.이 값은 MultiSelectList 환경 설정의 값입니다. 그러나 나는 그것을하고있는 것에 문제가있다, 여기 내 코드가 나를 도와 줘라.MulitSelectList 기본 설정의 각 배열 인덱스 값에 IF 문 추가

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 
} 

}

이이 selectedDoa가 배열 선택한 모든 인덱스를 확인해야합니다, 그래서 하나의 문자열 값이 아닌

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

답변

0

로 교체.

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 
} 
+0

이 작동합니다. 아주 많이 –

0

환경 조각에서 내 MultiSelectList입니다 이

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

우리의 바이든은 스위치 문자열이 이 시도

문제는

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

문자열이있는 스위치 블록은 API> = 19 (http://stackoverflow.com/a/19722741/606351)에서만 지원됩니다. 오래된 안드로이드의 경우 String을 int로 구문 분석하거나 String index = selectedDoa [I]에 .equals()가 포함 된 계단식 IF 문을 사용합니다 ( – Vaiden

+0

). 호환되지 않는 유형의 오류가 발생했습니다. 필수 : java.lang.String 발견 : Java.lang.CharSequence. 이걸 어떻게 수리해야합니까? –

+0

안녕하세요, 그것은 selectedDoa [i]입니다. 또한 코드가 잘 컴파일되어 있으며 android studio에서 테스트했습니다. mySharedPreferences.getStringSet ("selectedDoaKey", null); String [] selectedDoa = selectionsDoa.toArray (new String [] {}); for (int i = 0; i