android传递长字符串数组,在android中使用bundle传递数组

这篇博客介绍了如何在Android应用程序中使用Intent和Bundle传递字符串和整数数组。在发送方,可以通过Intent和Bundle将数组作为额外参数添加。在接收方,通过Intent获取额外数据并从中提取数组。示例代码展示了如何在不同Activity之间正确传递和接收数组。
摘要由CSDN通过智能技术生成

我需要将一个字符串/整数值数组从一个Activity传递给另一个Activity。 我该如何实现?

在活动A中:

String[] abc;

Bundle bundle =new Bundle();

bundle.putStringArray("some string",abc);

在您想要获得的活动B中,将代码指定为:

String abcd[]=bundle.getStringArray("some string");

两种情况下的"某些字符串"应该相同。

嘿,尼基谢谢!! ...但是您曾经尝试过吗,因为我访问过的各个站点都提到您无法使用捆绑包传递数组...是真的吗?

我当前的应用程序是通过这种方式完成的。 您尝试看看。

谢谢你尼基它的工作... :)

在发送方,代码应为:

String[] myStrings=new String[2];

myStrings[0]="MONDAY";

myStrings[1]="TUESDAY";

Intent intent = new Intent(v.getContext(), Animation_program.class);

Bundle bundle = new Bundle();

intent.putExtra("strings", myStrings);

intent.putExtras(bundle);

startActivity(intent);

在接收方,代码应为:

Intent i = getIntent();

Bundle extras=i.getExtras();

if(extras != null)  //this line is necessary for getting any value

{

String[] fajr_Values = i.getStringArrayExtra("strings");

Toast.makeText(this,"value="+fajr_Values[0]+""+fajr_Values[1], Toast.LENGTH_SHORT).show();

}

我从来没有使用捆绑包传递过一个数组,而且我不知道是否可以这样做,但是您当然可以传递一个ArrayList(或任何Serializable / Parcelable)。 查看此问题以获得更完整的答案:

在Android中的活动之间传递非基本类型的数据

从一个活动引用此传递数组列表可以对您有所帮助

请检查:stackoverflow.com/questions/55350646我无法将字符串数组数据传递给下一个滑动视图片段

传递字符串和整数值的代码::

在您的第一个活动中::

Intent intent = new Intent(California.this,details.class);

Bundle bundle = new Bundle();

bundle.putString("Keyname1", StringValue);

bundle.putInt("Keyname2", IntegerValue);

intent.putExtras(bundle);

startActivity(intent);

在第二活动中:

Bundle b=this.getIntent().getExtras();

String s=b.getString("Keyname1");

int i=b.getInt("Keyname2");

字符串和整数不是数组。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值