【问题标题】:How to pass array list between activities如何在活动之间传递数组列表
【发布时间】:2016-09-18 09:39:52
【问题描述】:

我有这个数组列表,我像这样从 json 解析列表项,

    List<String> imageUrls;

    imageUrls = new ArrayList<>();

    JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);

    for (int j = 0; j < imageArray.length(); j++) {
         String imageList = imageArray.getJSONObject(j).getString(labelKey).toString();
         imageUrls.add(imageList);
     }
    appShowModule.setAllimage(imageUrls);

然后我尝试在另一个活动中这样做,

                intent.putStringArrayListExtra("list", appShowModule.getAllimage());

但是“appShowModule.getAllimage()”是错误的!我怎样才能收到它?

【问题讨论】:

  • 我编辑我的问题
  • 你总是需要明确你的错误。 getAllimage() 返回什么?如果它返回一个通用的List,您需要将其转换为ArrayList&lt;String&gt; 才能在那里使用它 - (ArrayList&lt;String&gt;) appShowModule.getAllimage()。链接的副本在下一个Activity 中有如何检索它的示例。
  • 公开列表 getAllimageurl() { return Allimageurl; }
  • 在模块中我有这个私有 List Allimage = new ArrayList(); public List getAllimage() { return Allimage;} public void setAllimage(List allimage) { Allimage = allimage; }

标签: android arrays json arraylist


【解决方案1】:

你应该在你的课堂上实现Parcelable

查看这个最好的link以便于理解。

谢谢

【讨论】:

  • 我试试它不工作我不会其他解决方案谢谢
  • 是的,我在启动时也遇到了一些问题,您可以使用我指定的另一个链接。但是需要一些时间。如果问题仍然存在,则通过将其设为静态来进行序列化或直接访问
  • 在这种情况下你真的需要实现 Parcelable 吗?因为它是一个普通字符串列表@Androider
【解决方案2】:

将您的数组列表添加到您从中调用另一个活动的意图

 intent.putStringArrayListExtra("list", yourarraylist);
 startActivity(intent);

在activity中获取数组列表。

ArrayList list= new ArrayList();
list=  getIntent().getStringArrayListExtra("list");

希望这会对你有所帮助。

【讨论】:

    【解决方案3】:

    你可以在Intent使用这个方法:

    public Intent putExtra(String name, CharSequence[] value)
    

    【讨论】:

    • 那么?在方法中做什么?我不会将此数组传递给多个活动!怎么能这样?
    【解决方案4】:

    将 List imageUrls 更改为 ArrayList imageUrls 和 intent.putStringArrayListExtra("key",imageUrls);

    【讨论】:

    • 好的,我这样做了,但这不能应用 intent.putStringArrayListExtra("key",appShowModule.getAllimage());
    • 请告诉我 appShowModule.getAllimage() 它将返回什么。
    【解决方案5】:

    像这样发送您的列表

        Intent i = new Intent(this, YourDesiredActiity.class);
        i.putStringArrayListExtra("arrayListToSend",imageUrls);
        startActivity(i);
    

    然后在你想要的活动中,收到这样的列表

       ArrayList<String> urlList = new ArrayList<String>();
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            urlList.addAll(extras.getStringArrayList("arrayListToSend"));
        }
    

    然后你就可以在“urlList”中得到你想要的arrayList了

    【讨论】:

    • 像这样声明 imageUrls 变量 ArrayList urlList = new ArrayList();然后按照我的回答希望它会起作用
    猜你喜欢
    • 2013-07-22
    • 2019-05-02
    • 2013-12-31
    • 1970-01-01
    • 2012-10-30
    • 2012-03-22
    • 2019-01-07
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多