将两个包传递给相同的活动

时间:2016-11-18 16:08:04

标签: android json android-bundle

我有一个带有多个用户信息的json数组响应列表。创建了一个包并成功将其传递给下一个活动。使用用户选择的日期和时间创建了另一个包。但是第二个包到同一个活动没有运气,因为我只能将一个包传递给同一个活动。

我的实际问题是如何将putExtras中的第二个包传递给相同的活动

Json Response
{
  "userinfo": [
    {
      "address": "Tambaram",
      "name": "Vishranthi"
    },
    {
      "address": "Medavakkam",
      "name": "Sophia" 
    },
 ]
}

捆绑创建代码:

JSONArray infoarray = obj.getJSONArray("Info");
Bundle h = new Bundle();
for (int i = 0; i < infoarray.length(); i++) {
    Bundle b = new Bundle();

    JSONObject infoobject = infoarray.getJSONObject(i);
    String name = infoobject.getString("name");
    String address = infoobject.getString("address");

    b.putString("name", name);
    b.putString("address", address);

    h.putBundle(Integer.toString(i), b);

    System.out.println(b);
}
Intent i = new Intent(context, Secondpage.class);
Bundle d=new Bundle();
d.putString("date", text1);
d.putString("time", text2);

i.putExtras(h);
System.out.println(h);
context.startActivity(i);

5 个答案:

答案 0 :(得分:0)

你不能,所以最简单的解决方案是将你解析的json传递给第二个活动,将第二个包上想要的值作为基元传递,然后在第二个活动上将json解析为原始数据并检索日期和数据。时间。

答案 1 :(得分:0)

而不是创建另一个Bundle到日期和时间使用相同的

答案 2 :(得分:0)

您不需要反序列化JSONObject或JSONArray,只需将其作为String放在Bundle中。

Bundle bundle = new Bundle();
bundle.putString("MyBundle", infoarray.toString());

所以在下一个Activity中,获取JSONArray

JSONArray newJArray = new JSONArray(bundle.getString("MyBundle",""));

小提示:避免在循环内声明变量以避免内存泄漏。

答案 3 :(得分:0)

我认为你应该稍微改变一下方法: 在您的第一个活动中执行此操作您拥有json数组

rpatch = extractRotatedPatch(img, center, width, height, angle)

并在您的第二个活动中执行其余代码

 Intent i = new Intent(context, Secondpage.class);
        i.putExtra("userinfo", infoarray.toString());

答案 4 :(得分:0)

如果您想将两个捆绑包放入您的意图中,则必须使用

Intent putExtra (String name, 
                Bundle value)

类似的东西:

Intent i = new Intent(context, Secondpage.class);
i.putExtra("bundleH", h);
i.putExtra("bundleD", d);

ref

相关问题