2016-12-28 77 views
-1

我正在嘗試檢索微調器的選定位置。微調中的值是類型的字符串,我使用setOnItemSelectedListener改變類的字符串變量已選擇了一個和以後使用它:無法使用setOnItemSelectedListener獲取當前的旋轉器位置

spinner = (MaterialBetterSpinner) myToolbar.findViewById(R.id.spinner); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,socialMediaOptions); 
spinner.setAdapter(adapter); 
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      socialMedia = parent.getItemAtPosition(position).toString(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

private void sendPost(){ 
    Log.v(TAG, socialMedia); 
} 

的問題是變量當選擇的項目沒有變化,好像onItemSelected當存在微調選擇

+0

http://www.android-examples.com/get-selected-item-position-of-spinner-in-android/檢查此 –

+0

您可以從數組列表中獲取項目,而不是'socialMedia = socialMediaOptions.get(position )' – Redman

+0

in OnItemSelected'spinner.setSelection(selectedPosition)'希望這會對你有所幫助。 – Srihari

回答

0

希望您socialMediaOptions變化的字符串數組這樣

不會被調用
socialMediaOptions={"FB","TWIT","IN"} 

然後

//指定佈局時的選項列表出現

使用添加此行

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

    socialMedia = socialMediaOptions[position]; 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

    } 
    }); 
+0

試過,這應該工作,但它似乎** onItemSelected **不被稱爲 –