280
A
回答
597
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
25
你必須使用索引和適配器找出文本你有
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
相關問題
- 1. Android微調項目選定的文本
- 2. Android獲取選定的微調文本
- 3. 如何獲得微調選定項目
- 4. 微調選定項目
- 5. 如何從意圖中獲取微調器的選定項目
- 6. 獲取先前選定的項目 - 微調OnItemSelectedListener事件
- 7. 如何獲取Android微調器的選定項目?
- 8. 獲取微調器的選定項目,而不會導致java.nullPointerException
- 9. 如何將選定的微調項目獲取到字符串?
- 10. 我想微調器位置選定項目的文本
- 11. 滑動選定的項目文本在微調Android
- 12. 更改選定微調器項目的文本大小
- 13. 從微調獲取文本
- 14. 使用微調器的選定項目
- 15. Android微調選定項目與文本不匹配
- 16. 獲取選定的選項文本
- 17. 獲取選定的選項文本onChange
- 18. 微調onItemSelected與項目已經選定
- 19. Android微調器設置並獲取所選項目的標籤
- 20. Android工作室 - 微調 - 獲取微調項目不起作用
- 21. 將微調項目讀取到另一個微調項目
- 22. 如何獲取選擇控件中選定項目的文本?
- 23. 在微調的項目,如果選擇特定項目意味着只有編輯文本已是去實現,並獲得項目添加微調
- 24. 在選擇項目之前設置微調器的文本
- 25. 從微調框中獲取所選項目
- 26. 保存並獲取微調項目選擇
- 27. Android微調:獲取所選項目更改事件
- 28. Android微調選定項目獲取下一個活動開始時的變化
- 29. 如何從具有兩個項目的微調器中獲取選定值
- 30. AngularJS:獲取選定項目
@Harsha你是如何填充你的微調,粘貼一些代碼.... – Farhan 2011-04-29 04:12:39
已經張貼在這裏我的問題http://stackoverflow.com/questions/5818850/android-spinner-selected-item – 2011-04-29 15:46:29
我使用的代碼和結果不是我需要的調試模式,我發現它給了我像{supliers = VITA}的值。但我只需要價值「VITA」的任何想法? – 2012-03-01 21:07:12