在安卓中使用Bundle传递数组

17
我需要将一个包含字符串/整数值的数组从一个Activity传递到另一个Activity。我该如何实现这个功能?
5个回答

36

在活动 A 中:

String[] abc;

Bundle bundle =new Bundle();
bundle.putStringArray("some string",abc);

在想要获取代码的 B 活动中,请提供以下代码:

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

"some string"应该在两种情况下都是相同的。


嘿尼基,谢谢!!...但是你有没有试过这个?因为我访问过的各个网站都提到不能使用bundle传递数组...这是真的吗? - chetan

5
在发送方,代码应该是这样的:
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();
}

2

1

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

-2

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

在您的第一个Activity中::

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

1
字符串和整数不是数组。 - ozmank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接