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;
}
2
  • use this generic adapter Commented Nov 7, 2016 at 18:57
  • Just create Adapter class and extend with ArrayAdapter<>. And override GetView() method Commented Nov 7, 2016 at 18:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: Either convert your JSONArray into an ArrayList<Posts> 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.