使用putExtra传递多个变量

use*_*621 3 variables android android-activity

我有一些变量,我想传递给我的下一个活动,但我无法想办法做到这一点.

我的变量是:

JSONObject jsonObj = jsonArray.getJSONObject(i);
String propId = jsonObj.getString("id");
Log.i("Value id", propId);
String propCity = jsonObj.getString("city");
Log.i("Value city", propCity);
String propPlace = jsonObj.getString("place");
Log.i("Value place", propPlace);
String propStation = jsonObj.getString("station");
Log.i("Value station", propStation);
Run Code Online (Sandbox Code Playgroud)

我用来获取它们的代码是:

Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);
Run Code Online (Sandbox Code Playgroud)

有人可以帮我这个吗?

Mar*_*rno 5

您发布的代码是将它们写入Bundle,在您使用Bundle编写之后将捆绑.putExtras()包放入您的Intent.

intent.putExtras(bundle);
Run Code Online (Sandbox Code Playgroud)

例:

Intent intent = new Intent(this, YourClass.class);
Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);
intent.putExtras(bundle);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

要在活动中使用读它getExtras()得到Bundle你传递给它,然后使用getString/ getXXX.

无论如何,您可以避免创建Bundle并直接使用Intent以相同方式工作的set方法.

所以它会是:

Intent intent = new Intent(this, YourClass.class);
intent.putExtra("id", propId);
intent.putExtra("city", propCity);
intent.putExtra("place", propPlace);
intent.putExtra("station", propStation);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)