1

我有这个代码

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());

我正在获取我的包裹名称。

我如何获得我选择的项目

4

3 回答 3

1

采用

myIntent.putExtra("type", spinner.getSelectedItem().toString());

代替

myIntent.putExtra("type",spinner.getText().toString());

从 Spinner 获取选定的项目文本

于 2012-12-09T08:45:20.067 回答
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) {
        }
    })

然后,您可以使用将“选择”字符串放入您的意图中。祝你好运 :)

于 2012-12-09T13:53:39.887 回答
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) {
    }
});
于 2012-12-09T15:45:49.593 回答