java - 将 bundle 中的旋转器项目从一项 Activity 移动到另一项 Activity

标签 java android xml

我正在尝试从 Android 应用程序上的 Spinner 获取值并将其转换为字符串,以便我可以将其作为 Bundle 中的数据项移至另一个 Activity 。我已经成功地使用 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 结果名称; Text查看结果描述; TextView结果类型; TextView结果项目;

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 项目中收集数据吗?

最佳答案

为什么要传递不同的包?我猜,在您的接收 Activity 方面,您只获得了第一个 bundle 。

通过这些编辑尝试您的代码:

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

关于java - 将 bundle 中的旋转器项目从一项 Activity 移动到另一项 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426931/

相关文章:

Java:在循环中实例化变量:好还是坏的风格?

android - ion - 当前图像作为占位符

android - 平板电脑/手机上的 backbone.js 和调试

Java XMLStreamWriter : Outputting Unicode extended characters (non-BMP)

java - 空标签的 XML 验证

c++ - 使用 C++ 解析 EXI

java - 尝试从 Java 通过 HTTP-POST 发送到 GCM

java - 如何在浏览器中获取旧格式的 MediaWiki API 输出?

java - 定时状态机,这行得通吗?

android - 找不到 Android API 26 平台的地理编码器源