0

I'm attempting to use a JSON output from my blog. I'm getting the last 10 posts from my WordPress site and I'm able to get the data from it successfully in android and set it to a TextView. So I'm sure all of my networking works. Now, I need to make this a master detail layout. So in my master layout all I want to do is list the post_title, and in the detail I want to list the post_content. So this means I have to create an adapter. Where I'm getting confused is, what kind of adapter is the best to use? I'm not sure what kind of object I should use.
Below is my code that I use to get 1 of the post_titles and the post_content. Can anyone give me any direction on this? I was thinking that I could just use an ArrayList to hold post_titles, and another arrayList to hold post_content, but the post content can be very large. I was wondering if anyone could point me in the right direction?

I'm attempting use cwac-endless adapter so that I can load more blog posts, but it's just a wrapper. So I'm still stuck with creating my original adapter, and I'm still unsure of how to implement it.

public void parseJson(JsonObject result){
    JsonObject jsonPost = result.getAsJsonArray("posts").get(0).getAsJsonObject();  
    String title = jsonPost.get("title").getAsString();
    String content = jsonPost.get("content").getAsString();
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Note that [the documentation for CWAC-Endless](https://github.com/commonsguy/cwac-endless) has "Note that this has been tested with ArrayAdapter extensively but may not work with other adapter types, particularly SimpleAdapter". Advice like this may help steer you towards an appropriate `Adapter` implementation. – CommonsWare Jan 19 '14 at 13:38
  • I didn't know there was such a big difference between BaseAdapter and ArrayAdapter. I will read into them more later. – EGHDK Jan 19 '14 at 13:41

2 Answers2

2

I suggest you to use ArrayAdapter.

What you could do is:

Create a custom object for your blog output. Something like this

public class BlogItem{

private String title;
private String content;
//put getters and setters and all other member functions according to your application
}

Then create an adapter of BlogItem.

public class BlogAdapter extends ArrayAdapter<BlogItem> {
}

override the methods of ArrayAdapter, particularly it is always good to not to forget to override getCount() method and return the count of the items in the list.

See, if you have a custom object and arrayadapter like this, in future, even if you want to scale up, say, now with title and content you also want image (or some more text field), you can easily scale using the custom object. Also, when you get JSON response, just put the JSONdata into the object.

Hope this helps!

droidx
  • 2,172
  • 19
  • 26
0

Check this answer out that explains about ArrayAdapter, CursorAdapter and CustomAdapter

Community
  • 1
  • 1
VikramV
  • 1,111
  • 2
  • 13
  • 31
  • So adapters in Android can only be back by an Array or Cursor? – EGHDK Jan 18 '14 at 23:54
  • @EGHDK: No. `BaseAdapter` can be used to create a `ListAdapter` that adapts an arbitrary data model. However, using `BaseAdapter` is a bit more complex than is using `ArrayAdapter` or `CursorAdapter`, because you have to do more of the work. And, hence, [if your data model already is an array of objects](http://stackoverflow.com/q/21213657/115145), using `ArrayAdapter` is a better choice than is using `BaseAdapter`, as you save work while losing nothing. – CommonsWare Jan 19 '14 at 13:37