android中多个参数传递,android – 使用putExtra传递多个变量

这篇博客介绍了如何在Android应用程序中使用Intent的putExtra方法将多个变量(如JSON对象中的属性id、city、place和station)从一个活动传递到另一个活动。作者首先展示了获取变量的代码,然后提供了解决方案,包括创建Bundle或直接使用Intent的set方法来传递这些变量,并启动新的活动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

我的变量是:

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

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

Bundle extras = new Bundle();

extras.putString("id", propId);

extras.putString("city", propCity);

extras.putString("place", propPlace);

extras.putString("station", propStation);

有人可以帮我这个吗?

解决方法:

您发布的代码是将它们写入Bundle,在Bundle中写入后使用.putExtras()将您的bundle放入Intent中.

intent.putExtras(bundle);

例:

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

要在您的活动中阅读它,请使用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);

标签:android,variables,android-activity

来源: https://codeday.me/bug/20190725/1531030.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值