0

I am trying to create an ArrayAdapter for a custom JSONArray. I didn't find any tutorials I could use. The app is getting data from WordPress. It is getting all the posts, the class gets filled, but I don't know how to display the class into a list. It is declared as private Posts[] mPosts; So the array of posts get's parsed into an array of Posts objects; I have a List declared in my activity_main, I have a row.xml for the different objects. All that's left is the arrayadapter, which I can't seem to do. I can post MainActivity and XML files, but the post will be too long.

Posts.class

public class Posts {

private String mId;
private String mDate;
private String mTitle;
private String mContent;
private String mCategories;


public String getId() {
    return mId;
}

public void setId(String id) {
    mId = id;
}

public String getDate() {
    return mDate;
}

public void setDate(String date) {
    mDate = date;
}

public String getTitle() {
    return mTitle;
}

public void setTitle(String title) {
    mTitle = title;
}

public String getContent() {
    return mContent;
}

public void setContent(String content) {
    mContent = content;
}

public String getCategories() {
    return mCategories;
}

public void setCategories(String categories) {
    mCategories = categories;
}
James Jones
  • 3,850
  • 5
  • 25
  • 44
lakimens
  • 35
  • 10

1 Answers1

0

ArrayAdapter cannot adapt a JSONArray, as JSONArray is not a Java array ([] notation) and does not implement the java.util.List interface.

You are welcome to create a subclass of BaseAdapter that handles JSONArray.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I did see that question and I tried to mimick it, but because my Array is full of custom JSONObjects, I couldn't get the parameters of my Posts.class due to it not being JSONObject.getString(), I need to do Posts.getTitle(); and I couldn't move posts to the Adapter class. – lakimens Nov 07 '16 at 22:31
  • @lakimens: Either convert your `JSONArray` into an `ArrayList` and use `ArrayAdapter`, or delete `Posts` and use the `JSONArray` directly with a custom `BaseAdapter`. Or, better yet, use a better JSON parser (e.g., Gson), so you do not have a `JSONArray` in the first place. – CommonsWare Nov 07 '16 at 22:41
  • My bad, I don't have a JSONArray, I have an array of JSONObjects(Posts.class), it's a java array(mPosts[]), not a JSONArray, I still haven't made the adapter though. – lakimens Nov 09 '16 at 21:50