2011-04-26 87 views

回答

597
Spinner spinner = (Spinner)findViewById(R.id.spinner); 
String text = spinner.getSelectedItem().toString(); 
+0

@Harsha你是如何填充你的微調,粘貼一些代碼.... – Farhan 2011-04-29 04:12:39

+0

已經張貼在這裏我的問題http://stackoverflow.com/questions/5818850/android-spinner-selected-item – 2011-04-29 15:46:29

+0

我使用的代碼和結果不是我需要的調試模式,我發現它給了我像{supliers = VITA}的值。但我只需要價值「VITA」的任何想法? – 2012-03-01 21:07:12

25

你必須使用索引和適配器找出文本你有

this example of Spinner

public class MyOnItemSelectedListener implements OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, 
     View view, int pos, long id) { 
     Toast.makeText(parent.getContext()), "The planet is " + 
      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
    } 

    public void onNothingSelected(AdapterView parent) { 
     // Do nothing. 
    } 
} 
+2

還需要'spinner.setOnItemSelectedListener(本);' – whiteLT 2013-09-17 19:19:16

10

微調將返回該陣列的整數值。您必須根據索引檢索字符串值。

Spinner MySpinner = (Spinner)findViewById(R.id.spinner); 
Integer indexValue = MySpinner.getSelectedItemPosition(); 
13

使用本

import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class dynamic_spinner_main extends Activity { 

    private Spinner m_myDynamicSpinner; 
    private EditText m_addItemText; 
    private ArrayAdapter<CharSequence> m_adapterForSpinner; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_spinner); 

     /////////////////////////////////////////////////////////////// 
     //grab our UI elements so we can manipulate them (in the case of the Spinner) 
     // or add listeners to them (in the case of the buttons) 
     m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);   
     m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText); 
     Button addButton = (Button)findViewById(R.id.AddBtn); 
     Button clearButton = (Button)findViewById(R.id.ClearBtn); 

     //////////////////////////////////////////////////////////////// 
     //create an arrayAdapter an assign it to the spinner 
     m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item); 
     m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
     m_myDynamicSpinner.setAdapter(m_adapterForSpinner); 
     m_adapterForSpinner.add("gr");   
     m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
       // your code here 
       Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class); 
       mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position)); 
       System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString()); 
       startActivity(mIntent); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parentView) { 
       // your code here 
      } 

     }); 
     //////////////////////////////////////////////////////////////// 
     //add listener for addButton 
     addButton.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) {    
       addNewSpinnerItem(); 
      }     
     }); 

     //////////////////////////////////////////////////////////////// 
     //add listener for addButton 
     clearButton.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       clearSpinnerItems(); 
      }   
     }); 
    } 

    private void addNewSpinnerItem() { 
     CharSequence textHolder = "" + m_addItemText.getText(); 
     m_adapterForSpinner.add(textHolder); 
    } 

    private void clearSpinnerItems() { 
     m_adapterForSpinner.clear(); 
     m_adapterForSpinner.add("dummy item"); 
    }  
} 

main_spinner.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <EditText android:layout_height="wrap_content" 
      android:layout_margin="4px" 
      android:id="@+id/newSpinnerItemText" 
      android:layout_width="fill_parent"></EditText> 
    <Button android:layout_height="wrap_content" 
      android:id="@+id/AddBtn" 
      android:layout_margin="4px" 
      android:layout_width="fill_parent" 
      android:text="Add To Spinner"></Button> 
    <Button android:layout_height="wrap_content" 
      android:id="@+id/ClearBtn" 
      android:layout_margin="4px" 
      android:layout_width="fill_parent" 
      android:text="Clear Spinner Items"></Button> 
    <Spinner android:layout_height="wrap_content" 
      android:id="@+id/dynamicSpinner" 
      android:layout_margin="4px" 
      android:layout_width="fill_parent"></Spinner> 
</LinearLayout> 
32
TextView textView = (TextView)mySpinner.getSelectedView(); 
String result = textView.getText().toString(); 
+5

你應該總是包含你提出的解決方案的解釋 - http://stackoverflow.com/questions/how-to-answer – Michal 2012-03-17 16:28:33

7
TextView textView = (TextView) spinActSubTask.getSelectedView().findViewById(R.id.tvProduct); 

String subItem = textView.getText().toString(); 
8

一個行版本:

String text = ((Spinner)findViewById(R.id.spinner)).getSelectedItem().toString(); 

更新: 如果您使用SDK 26(或更新版本)編譯項目,則可以刪除轉換。

String text = findViewById(R.id.spinner).getSelectedItem().toString(); 
7
spinner_button.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?>arg0, View view, int arg2, long arg3) { 

      String selected_val=spinner_button.getSelectedItem().toString(); 

      Toast.makeText(getApplicationContext(), selected_val , 
        Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 

} 
3

對於基於一個CursorAdapter紡紗:

  • 獲得所選項目編號:spinner.getSelectedItemId()
  • 從數據庫中獲取的項目名稱,例如:

    public String getCountryName(int pId){ 
        Cursor cur = mDb.query(TABLE, new String[]{COL_NAME}, COL_ID+"=?", new String[]{pId+""}, null, null, null); 
        String ret = null; 
        if(cur.moveToFirst()){ 
         ret = cur.getString(0); 
        } 
        cur.close(); 
        return ret; 
    } 
    
1

對於那些已經基於HashMap的微調:

((HashMap)((Spinner)findViewById(R.id.YourSpinnerId)).getSelectedItem()).values().toArray()[0].toString(); 

如果你是在一個片段,適配器或大於主要活動以外的其他類,使用此:

((HashMap)((Spinner)YourInflatedLayoutOrView.findViewById(R.id.YourSpinnerId)).getSelectedItem()).values().toArray()[0].toString(); 

它只是爲指導;您應該在onClick方法之前找到您的視圖的ID爲

-1
Spinner spinner = (Spinner) findViewById(R.id.yourspinnerid); 
String text = spinner.getSelectedItem().toString(); 
+2

這已經發布完全像這樣... – bish 2015-12-27 11:49:31

+0

這可能很好地解決了這個問題,但也請提供一個解釋。許多新用戶來到SO並用您的代碼進行解釋,這有助於他們學習如何調整代碼以解決問題。 – JenB 2015-12-27 12:18:41

相關問題