Deniz Celebi
Deniz Celebi

Reputation: 908

Android GSON access List in ArrayList

I'm using GSON to parse a JSON feed like this here:

http://dvz.hj.cx/api/get_recent_posts/?dev=1

My model class looks like this one here:

public class Recent {

@Expose
private String status;
@Expose
private int count;
@Expose
private int count_total;
@Expose
private int pages;  
@Expose
private List<Post> posts = new ArrayList<Post>();

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Recent withStatus(String status) {
    this.status = status;
    return this;
}

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public Recent withCount(int count) {
    this.count = count;
    return this;
}

public int getCount_total() {
    return count_total;
}

public void setCount_total(int count_total) {
    this.count_total = count_total;
}

public Recent withCount_total(int count_total) {
    this.count_total = count_total;
    return this;
}

public int getPages() {
    return pages;
}

public void setPages(int pages) {
    this.pages = pages;
}

public Recent withPages(int pages) {
    this.pages = pages;
    return this;
}  

public List<Post> getPosts() {
    return posts;
}

public void setPosts(List<Post> posts) {
    this.posts = posts;
}

public Recent withPosts(List<Post> posts) {
    this.posts = posts;
    return this;
 }
}

As you can see I'm referring to another model class called Post. The Post model class looks like this one:

public class Post {

@Expose
private int id;
@Expose
private String url;
@Expose
private String title;
@Expose
private String date;
@Expose
private List<Category> categories = new ArrayList<Category>();
@Expose
private List<Object> tags = new ArrayList<Object>(); 
@Expose
private Author author;
@Expose
private List<Attachment> attachments = new ArrayList<Attachment>();

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Post withId(int id) {
    this.id = id;
    return this;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public Post withUrl(String url) {
    this.url = url;
    return this;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Post withTitle(String title) {
    this.title = title;
    return this;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public Post withDate(String date) {
    this.date = date;
    return this;
}

public List<Category> getCategories() {
    return categories;
}

public void setCategories(List<Category> categories) {
    this.categories = categories;
}

public Post withCategories(List<Category> categories) {
    this.categories = categories;
    return this;
}

public Author getAuthor() {
    return author;
}

public void setAuthor(Author author) {
    this.author = author;
}

public Post withAuthor(Author author) {
    this.author = author;
    return this;
}

public List<Attachment> getAttachments() {
    return attachments;
}

public void setAttachments(List<Attachment> attachments) {
    this.attachments = attachments;
}

public Post withAttachments(List<Attachment> attachments) {
    this.attachments = attachments;
    return this;
 }
}

And again I'm reffering to some other models. Until now erverything works perfect, but now I need to access some of this getters and setters in my BaseAdapter. My Adapter classe looks like this:

public class NewsList extends BaseAdapter {

private List<Recent> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public ImageLoader imageLoader;

public NewsList(Context context, List<Recent> listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
    mContext = context;

    imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.news_row_layout, null);
        holder = new ViewHolder();
        holder.headlineView = (TextView) convertView.findViewById(R.id.title);
        holder.commentView = (TextView) convertView.findViewById(R.id.comment);
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
        holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Recent rec = (Recent) listData.get(position);
    Post post = (Post) rec.getPosts();
    Attachment att = (Attachment) post.getAttachments();
    List<Images> img = att.getImages();
    Thumbnail thumb = (Thumbnail) img.getThumbnail();
    Author author = (Author) post.getAuthor();

          if(post != null){

        /* date and time */
        String date = post.getDate().replace("-",".");
        String zeit = date.substring(11,16);
        String datum = date.substring(0, 11);
        String djahr = datum.substring(0,4);
        String dmonat = datum.substring(5,8);
        String dtag = datum.substring(8,10);

        holder.headlineView.setText(Html.fromHtml(post.getTitle()));
        holder.reportedDateView.setText(Html.fromHtml("Am <b>" + dtag+"."+dmonat+djahr+" </b>um <b>"+zeit+"</b>"));
        holder.commentView.setText(Html.fromHtml("Von: <b>"  + author.getName()));


        ImageView image = holder.imageView;
        if(post.attachments.getMime_type().contains("image")){
            imageLoader.DisplayImage(thumb.getUrl(), image);
        }
    }
    return convertView;
}


static class ViewHolder {
    TextView headlineView;
    TextView commentView;
    TextView reportedDateView;
    ImageView imageView;
}
}

As you see I try to get the List<Post> which is located inside the ArrayList<Recent>.

This line works perfect:
Recent rec = (Recent) listData.get(position);

But as soon as it comes to this line it doesn't work:
Post post = (Post) rec.getPosts();

I have no idea how to resolve this. Please help its very important for me. If you have a better solution, its welcome.

When it comes to this line Post post = (Post) rec.getPosts();, LogCat says

Cannot convert ArrayList to List

Upvotes: 0

Views: 440

Answers (1)

Software Engineer
Software Engineer

Reputation: 3956

You are misinterpreting List<T> with T and this same problem is present at different parts of your code:

getPosts() returns List<Post> not Post like getImages() returns List<Images> not Images, you might need a loop to iterate over your List<Post>, getting single Post and then getting its data like List<Attachment>.

Change it to the following:

Recent rec = (Recent) listData.get(position);
List<Post> posts = rec.getPosts();

for(int i = 0; i < posts.size(); i++){

  Post post = (Post) posts.get(i);
  if(post != null){
    List<Attachment> atts = post.getAttachments();
    Attachment att = (Attachment) atts.get(0) // You can use loop instead of get(0)
    List<Images> imgs = att.getImages();
    Images img = (Images) imgs.get(0); // You can use loop instead of get(0)
    Thumbnail thumb = (Thumbnail) img.getThumbnail();
    Author author = (Author) post.getAuthor();    
    ...
  }
}

Upvotes: 1

Related Questions