2011-12-08 42 views
2

我试图编写一种方法将两个微调器的值传递给另一个活动。传递微调器值

我在网上找到了使用putExtra方法的例子,但是我自己实现时遇到了问题(我想我只是不清楚它是如何工作的)。

我的代码是(没有在putExtra方法的任何值,因为这是我卡上的位):

public void addListenerOnButton() { 

     transportSpinner = (Spinner) findViewById(R.id.transportSpinner); 
     locationSpinner = (Spinner) findViewById(R.id.locationSpinner); 

     buttonSubmit = (Button) findViewById(R.id.buttonSubmit); 

     buttonSubmit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class); 
       i.putExtra(); 
       GetDirections.this.startActivity(i); 

      } 

     }); 
} 

感谢。

+0

究竟是你想做些什么?例如,你在“Intent”中包装了什么? – Phil

+0

哪个适配器与微调器一起使用? – havexz

回答

1

假设您的纺纱厂已正确安装,你可以这样做:

... 
i.putExtra("transportSpinnerSelected", transportSpinner.getSelectedItem()); 
i.putExtra("locationSpinnerSelected", locationSpinner.getSelectedItem()); 
... 
+0

非常感谢,这真的很有帮助。 – djcmm476

+0

没问题,很高兴帮助。如果您将答案标记为正确,那很好,谢谢! – gwa

2

大厦GWA的回答是:

在此屏幕上,你需要做的(假设你的纺纱厂的意图有正确的价值观当然):

Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class); 
i.putExtra("transportSpinnerValue", transportSpinner.getSelectedItem().toString()); 
i.putExtra("locationSpinnerValue", locationSpinner.getSelectedItem().toString()); 
GetDirections.this.startActivity(i); 

然后在下一屏幕,你必须检索这些值。既然你在Intent中传递了额外的信息,你必须从下一课的意图中得到它。

所以做:

//DirectionsDisplay class 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle extras = getIntent().getExtras(); 
    String transportItemChosen = extras.getString("transportSpinnerValue"); 
    String locationItemChosen = extras.getString("locationSpinnerValue"); 

} 
+0

你是英雄,编码和android的英雄。干杯。 – djcmm476