我需要将一个字符串/整数值数组从一个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");
字符串和整数不是数组。