【问题标题】:How to pass spinner data from one activity to another ?如何将微调器数据从一个活动传递到另一个活动?
【发布时间】:2014-02-21 08:08:08
【问题描述】:

此代码不是从微调器中读取值,而是始终只读取第一个值,

btnResult.setOnClickListener(new View.OnClickListener() 
{
    final String USN = spnConversions.getSelectedItem().toString();
    @Override
    public void onClick(View v) 
    {
        Intent i = new Intent(getApplicationContext(), DatabaseResult.class);
        i.putExtra("getData",USN.toString());
        startActivity(i);
    }
});

【问题讨论】:

  • 你想一次传递一个数据还是整个微调器?
  • 我只想传递一个值一次。这就像如果我选择 10002 它总是传递第一个值 10001 总是。(我已经按顺序给出了这些数字)

标签: android android-intent android-activity spinner


【解决方案1】:

为什么你使用 onClickListener 作为 Spinner ?您应该对 Spinner 使用 OnItemSelectedListener() 参见下面的示例代码,

public class MySpinnerSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

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

现在使用以下代码注册监听器,

 spinner.setOnItemSelectedListener(new MySpinnerSelectedListener());

您可以使用以下代码传递它,

// 发送代码

Intent intent = new Intent(getApplicationContext(), DatabaseResult.class);
intent.putextra("getData",USN.toString());
startActivity(intent);

//接收代码,

String value= getIntent().getStringExtra("getData");

【讨论】:

  • 我在另一个活动中使用了同样的东西。这不是问题,但是每当我从微调器中选择 10002 时,它都不会从微调器中获取位置值。我需要在第一个活动中添加任何代码吗?
  • 谢谢,我做了一些改动,只是我从 spinner.onItemSelectedListener() 调用了我的 btn.onClickListener(),它正在工作。
  • 感谢您宝贵的时间。
【解决方案2】:
public class SpinnerExample extends Activity
{
     Spinner sp;
     String text ="";
     Button btnResult;

     public void onCreate(Bundle savedInstanceState)
     {
         sp = (Spinner) findViewById(R.id.spinner1);
         sp.setOnItemSelectedListener(new OnItemSelectedListener() {
                   public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3)
                   {
                     this.text = parent.getItemAtPosition(pos).toString();

                   }
                   public void onNothingSelected(AdapterView<?> arg0)
                   {
                            / TODO Auto-generated method stub                  
                   }
          });
          btnResult = (Button) findViewById(R.id.buttonId);
          btnResult.setOnClickListener(new View.OnClickListener() 
          {

                   @Override
                   public void onClick(View v) 
                  {
                        Intent i = new Intent(getApplicationContext(), DatabaseResult.class);
                        i.putExtra("getData",this.text);
                        startActivity(i);
                  }
           });
    }
}

【讨论】:

    【解决方案3】:

    试试这个

    int positionitem = spinner.getSelectedItemPosition();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-31
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多