2012-09-14 194 views
2

我试图从我的android应用程序的微调器获取值并将其转换为字符串,以便我可以将它作为一个包中的数据项移动到另一个活动。我已经成功设法使用getText().toString();方法的组合移动EditText值。我现在正在寻找同样的结果,但现在却与Spinner项目有关,但目前为止没有成功。将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();没有得到从代码的项目和种类线结果项目也是如此。

下面显示的是从输入表单接收和输出此数据的活动的代码。

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

} 

任何人有任何想法如何成功收集数据从微调项目?

回答

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

稀释是谢谢!我首先想到了单独的捆绑包,但我并没有将它们放入额外资源中。但仅使用一个包就可以节省复杂性和代码重复。感谢您的帮助,非常感谢:) – WebDevDanno

+0

可能编辑代码以适应将图像放入Bundle以及?我需要能够将有问题的建筑物的图片移动到下一个活动...尝试使用'byte []'变量,但有点失落! – WebDevDanno

+0

我建议你创建一个新的问题,以便所有的SO用户都能从中受益。 – stpn108

相关问题