将Bundle中的微调器项从一个活动移动到另一个活动

时间:2012-09-14 14:51:36

标签: java android xml

我正在尝试从我的Android应用程序上的Spinner获取值并将其转换为String,以便我可以将其作为Bundle中的数据项移动到另一个活动。我已成功设法使用getText().toString();方法的组合移动EditText值。我正在寻找相同的结果,但现在使用Spinner项目,但到目前为止还没有成功。

以下是代码:

当用户选择onClick方法中的按钮时,将调用此方法:

public void commitData(){
    Bundle bundle = new Bundle();
    bundle.putString("key", txtBuildingName.getText().toString()); //Gets the TEXT that the TEXTVIEW was holding converts it to a String and adds to the Extras bundle

    Bundle bundle1 = new Bundle();
    bundle1.putString("key1", txtDescription.getText().toString()); // Same again

    Bundle bundle2 = new Bundle();
    bundle2.putString("key2", type.getItemAtPosition(type.getSelectedItemPosition()).toString());

    Bundle bundle3 = new Bundle();
    bundle3.putString("key3", project.getItemAtPosition(project.getSelectedItemPosition()).toString());

    Intent newIntent = new Intent(this.getApplicationContext(), DataSummary.class);
    newIntent.putExtras(bundle);
    newIntent.putExtras(bundle1);
    startActivityForResult(newIntent, 0);
}

我从项目中得不到任何结果,并使用type.getItemAtPosition().getSelectedItemPosition()).toString();输入代码行,对项目也是如此。

下面显示的是从条目表单接收和输出此数据的Activity的代码。

TextView resultName;     TextView resultDescription;     TextView resultType;     TextView resultProject;

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.activity_data_summary);

   //Check if there is anything in the 'bundle' and if not produce message - AVOIDS NULLPOINTEREXCEPTION when navigating to Activity
   Bundle bundle = this.getIntent().getExtras();     
   if (bundle != null){       
   String name = bundle.getString("key");
   String description = bundle.getString("key1"); //gets data from DataEntry activity
   String type = bundle.getString("key2");
   String project = bundle.getString("key3");

   resultName=(TextView)findViewById(R.id.resultName);  //adds the TextViews to the activity
   resultType=(TextView)findViewById(R.id.resultType);
   resultDescription=(TextView)findViewById(R.id.resultDesc);
   resultProject=(TextView)findViewById(R.id.resultProject);

   resultName.setText(name); // Fills the textviews with imported data
   resultType.setText(type);
   resultDescription.setText(description);
   resultProject.setText(project);
   }    

   else
   {
       Toast.makeText(DataSummary.this,"Received no data yet!", Toast.LENGTH_LONG).show();
   }

}

任何人都有任何想法如何成功从Spinner项目中收集数据?

1 个答案:

答案 0 :(得分:4)

为什么要传递不同的捆绑包?在接收活动的一侧,我猜你只得到第一个捆绑包。

尝试使用以下修改代码:

public void commitData(){

    Bundle bundle = new Bundle();
    bundle.putString("key", txtBuildingName.getText().toString()); //Gets the TEXT that the TEXTVIEW was holding converts it to a String and adds to the Extras bundle
    bundle.putString("key1", txtDescription.getText().toString()); // Same again
    bundle.putString("key2", type.getItemAtPosition(type.getSelectedItemPosition()).toString());
    bundle.putString("key3", project.getItemAtPosition(project.getSelectedItemPosition()).toString());