我有这个代码
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText().toString());
myIntent.putExtra("type", spinner.getText().toString());
LoginActivity.this.startActivity(myIntent);
我希望微调器中的选定选项为我的字符串。
这给我一个错误
myIntent.putExtra("type", spinner.getText().toString());
当我使用
时myIntent.putExtra("type", spinner.getContext().toString());
我收到我的包裹名称。
如何获取所选项目
答案 0 :(得分:1)
使用
myIntent.putExtra("type", spinner.getSelectedItem().toString());
而不是
myIntent.putExtra("type",spinner.getText().toString());
从Spinner获取所选项目文本
答案 1 :(得分:0)
您必须向微调器添加一个侦听器才能获取所选项。它应该是这样的
String selection = null;
//Initialise Spinner (you may have done this already)
Spinner spinner = (Spinner) findViewById(R.id.[your spinner id]);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selection = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
})
然后,您可以将“选择”字符串放入您的意图中。祝你好运:)
答案 2 :(得分:0)
只需检查此代码,我希望此代码有用,
String getSelectItem = "null";
Spinner spinner =(Spinner)findViewById(R.id。[your spinner id]);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
getSelectItem = spinner.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});