2017-03-15 11 views
1

은 내가이 배열어떻게? 안드로이드

준이는 스피너 선택에 안드로이드

에 따라서

Spinner ctype = (Spinner) findViewById(R.id.s1); 
String ct=ctype.getSelectedItem().toString(); 

프로그램 -을 스피너를 추가 감안할있다

XML--

<Spinner 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/s1" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:entries="@array/ct_array" 
     android:prompt="@string/ct" 
     android:layout_marginTop="2dp" 
     android:layout_below="@+id/tt1"/> 

Strings--

0 그것은 ftp를 우리는 방법에 따라 변경할 수 있습니다 기본 value..So로 .. 그래서 여기

은 내가 1 Spinner을 감안할 때 한 점에서 설정 페이지가 표시 선택에 따라서

<string-array name="ct_array"> 
     <item>ftp://</item> 
     <item>http://</item> 
     <item>https://</item> 
     <item>other</item> 
    </string-array> 

, 3 EditText 및 공유의 환경 설정의 도움으로 나는 텍스트 편집을 위해 .. 내가 이전 SharedValues을 읽고있다 map.get("xyz")의 도움으로

나는 최근에 그 공유 Prefes의 편집 페이지를 추가 한

...

... 그들을 저장하고 나는 이

EditText value1= (EditText) findViewById(R.id.et1); 
value1.setText(map.get("value1")); 

그래서 그것의 내가 스피너 그러나 대한 추가 할 같은 방식으로 이전 저장된 값을 표시 그것의 항상 FTP는 어떤 사람이 나에게

나는이 함께 노력

을 제안 할 수 있지만, 그것은 작동하지 않습니다 표시

Spinner conntype = (Spinner) findViewById(R.id.s1); 
    conntype.setSelection(map.get("conntype")); 
         // here it should be **(int)** not **(java.lang.stirng)** 

여기 이전 값이 http:// 인 경우 이전 선택 값을 얻으려고합니다. http://을 표시하려면 변경자를 변경하려면 변경할 수 있지만 기본값은 이전에 선택한 사용자 여야합니다.

이 종류에 어떤 제안

답변

1

는 아래의 코드를 사용하십시오 : 여기

int spinnerPosition = getIndex(conntype, map.get("conntype")); 
conntype.setSelection(spinnerPosition); 

을 getIndex() 방법이 도움이

private int getIndex(Spinner spinner, String value){ 

    for (int i=0;i<spinner.getCount();i++){ 
     if (spinner.getItemAtPosition(i).equals(value)){ 
     return i; 
     } 
    } 
    return 0 ; 
    } 

희망.

+0

감사합니다. @tahsinRupam 선생님, 여기 '어댑터'란 무엇입니까? 'mSpinner'에 대해서 제 코드에 따라'conntype'을주었습니다'Spinner conntype = (Spinner) findViewById (R.id.s1); conntype.setSelection (map.get ("conntype")); ' –

+0

내 코드로 응답을 업데이트 할 수 있습니까? –

+1

감사합니다. 작동합니다 ..... 제 질문에 투표하십시오. –