使用bundle传递多个数组

时间:2014-01-22 09:10:05

标签: java android arrays bundle

我正在尝试将2个双数组从一个活动传递到另一个活动。但是,当我尝试将第一个活动中的2个数组中的值传递给第二个活动中的数组时,我只得到第一个数组中的值,并将其存储在两个新数组中。

这就是我使用bundle发送数组的方法

Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
intent.putExtras(bund);
Bundle bund2 = new Bundle();
bund2.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund2);        
startActivity(intent);

接收方我有:

Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
Bundle bund2 = intent.getExtras();
endLongitude = bund2.getDoubleArray(endLongitudeStr);

但是结果总是只是第一个数组的值(在本例中为endLatitude) 我做错了什么?

3 个答案:

答案 0 :(得分:2)

使用相同的捆绑对象。

Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
bund.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund);        
startActivity(intent);

Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
endLongitude = bund.getDoubleArray(endLongitudeStr);

答案 1 :(得分:0)

如果我没记错,你只能使用一个捆绑包,因为如果你再制作另一个捆绑包,它将取代之前的捆绑包,所以你需要做的是将bund1和bun2放在第一个捆绑包上然后检索它 使用

Bundle bundle = new Bundle();
bundle = getIntent().getExtras();
String mystring=bundle.getString("bund1");
String mystring=bundle.getString("bund2");

答案 2 :(得分:0)

为什么使用2套装?只需使用一个......

Bundle bundle = new Bundle();
bundle.putDoubleArray(endLatitudeStr, endLatitude);
bundle.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtra(bundle);        
startActivity(intent);

和......

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
endLatitude = bundle.getDoubleArray(endLatitudeStr);
endLongitude = bundle.getDoubleArray(endLongitudeStr);