我需要将一个包含字符串/整数值的数组从一个Activity传递到另一个Activity。我该如何实现这个功能?
在活动 A 中:
String[] abc;
Bundle bundle =new Bundle();
bundle.putStringArray("some string",abc);
在想要获取代码的 B 活动中,请提供以下代码:
String abcd[]=bundle.getStringArray("some string");
"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();
}
参考这个如何在不同的Activity之间传递ArrayList可能会对您有所帮助
传递字符串和整数的代码::
在您的第一个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");