如何将JSONarray添加到android中的arraylist

时间:2013-11-13 13:21:03

标签: android arraylist

尝试向arraylist添加5个JSONarrays,但收到此错误

The method add(AllMessageObject) in the type ArrayList<AllMessageObject> is not applicable for the arguments (String)

这里我放置代码将jsonarray添加到arraylist.how来修复它。

for (int j = 0; j < a; j++) {
listdata  = new ArrayList<AllMessageObject>();

listdata.add(jsonArray.get(j).toString());

}

AllMessageObject是一个类,这是我对该类的代码。而listdata是我的数组列表ArrayList<AllMessageObject> listdata = null; 我希望这段代码足够了,如果你需要更多我会在这里提供它。

public class AllMessageObject {

// for getting comment
public String message;
public String from_name;
public String message_date;

// for getting picture 
public String picture_url;
public Bitmap picturein_bitmap;

// for getting my profile image
public String fromprofile_url;
//public Bitmap fromprofileurl_bitmap;


public String getFromprofile_url() {
    return fromprofile_url;
}
public void setFromprofile_url(String fromprofile_url) {
    this.fromprofile_url = fromprofile_url;
}
/*public Bitmap getFromprofileurl_bitmap() {
    return fromprofileurl_bitmap;
}
public void setFromprofileurl_bitmap(Bitmap fromprofileurl_bitmap) {
    this.fromprofileurl_bitmap = fromprofileurl_bitmap;
}*/
public Bitmap getPicturein_bitmap() {
    return picturein_bitmap;
}
public void setPicturein_bitmap(Bitmap picturein_bitmap) {
    this.picturein_bitmap = picturein_bitmap;
}
public String getPicture_url() {
    return picture_url;
}
public void setPicture_url(String picture_url) {
    this.picture_url = picture_url;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public String getFrom_name() {
    return from_name;
}
public void setFrom_name(String from_name) {
    this.from_name = from_name;
}
public String getMessage_date() {
    return message_date;
}
public void setMessage_date(String message_date) {
    this.message_date = message_date;
}



}

1 个答案:

答案 0 :(得分:0)

您正尝试将String放入AllMessageObject ArrayList中。此外,每次循环运行时,您都会分配新的ArrayList,否则将丢失先前添加的值。

所以请尝试这样:

listdata  = new ArrayList<String>();
for (int j = 0; j < a; j++) {   
    listdata.add(jsonArray.get(j).toString());
}