【问题标题】:Spinner selection to String微调器选择到字符串
【发布时间】:2012-12-09 08:41:52
【问题描述】:

我有这个代码

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

我正在获取我的包名。

我如何获得我选择的项目

【问题讨论】:

    标签: java android string android-intent spinner


    【解决方案1】:

    使用

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

    代替

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

    用于从 Spinner 获取所选项目的文本

    【讨论】:

      【解决方案2】:

      您必须向微调器添加一个侦听器才能获取所选项目。应该是这样的

      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) {
              }
          })
      

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

      【讨论】:

        【解决方案3】:

        只需检查此代码,我希望此代码有用,

        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) {
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2016-11-07
          • 1970-01-01
          • 2012-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-11
          相关资源
          最近更新 更多