我怎样才能将数组放入类中

时间:2017-11-21 21:16:09

标签: android android-studio

我试图将数组放入类

ArrayList<String> list = new ArrayList<>();

                item_messages.add(new Item_Messages(response.getJSONArray("posts").getJSONObject(x).getString("id"),response.getJSONArray("posts").getJSONObject(x).getString("userid"),response.getJSONArray("posts").getJSONObject(x).getString("authorname"),response.getJSONArray("posts").getJSONObject(x).getString("message"),String.valueOf(response.getJSONArray("posts").getJSONObject(x).getJSONArray("Likes").length()),response.getJSONArray("posts").getJSONObject(x).getString("type"),list.get(0)));

但是有问题

enter image description here

我的班级

package com.salah.mymessages.whatsapp;


import java.util.ArrayList;

public class Item_Messages {


    String id,userid,authorname,message,likes,type;
    ArrayList<String> list = new ArrayList<>();

    public Item_Messages(String id, String userid, String authorname, String message, String likes, String type, ArrayList<String> list) {
        this.id = id;
        this.userid = userid;
        this.authorname = authorname;
        this.message = message;
        this.likes = likes;
        this.type = type;
        this.list = list;
    }
}

希望你帮助我

1 个答案:

答案 0 :(得分:1)

试试这个

package com.salah.mymessages.whatsapp;

import java.util.ArrayList;
import java.util.List;

public class Item_Messages {


String id,userid,authorname,message,likes,type;
List<String> list = new ArrayList<>();

public Item_Messages(String id, String userid, String authorname, String message, String likes, String type, List<String> list) {
    this.id = id;
    this.userid = userid;
    this.authorname = authorname;
    this.message = message;
    this.likes = likes;
    this.type = type;
    this.list = list;
    }
}

这个

item_messages.add(new Item_Messages(response.getJSONArray("posts").getJSONObject(x).getString("id"),response.getJSONArray("posts").getJSONObject(x).getString("userid"),response.getJSONArray("posts").getJSONObject(x).getString("authorname"),response.getJSONArray("posts").getJSONObject(x).getString("message"),String.valueOf(response.getJSONArray("posts").getJSONObject(x).getJSONArray("Likes").length()),response.getJSONArray("posts").getJSONObject(x).getString("type"),list));

构造函数中的最后一个参数需要一个List,你从列表(list.get(0))中放入第一个String