gpt4 book ai didi

java - Android GSON访问ArrayList中的列表

转载 作者:行者123 更新时间:2023-12-01 12:50:24 25 4
gpt4 key购买 nike

我使用 GSON 来解析 JSON 提要,如下所示:

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

我的模型类看起来像这样:

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;
}
}

如您所见,我指的是另一个名为 Post 的模型类。 。Post模型类看起来像这样:

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;
}
}

我再次提到了一些其他模型。到目前为止,一切都很完美,但现在我需要访问我的 BaseAdapter 中的一些 getter 和 setter 。我的适配器类如下所示:

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;
}
}

如你所见,我尝试获取 List<Post>位于 ArrayList<Recent> 内.

这条线工作完美:
Recent rec = (Recent) listData.get(position);

但是一旦到了这一行就不行了:
Post post = (Post) rec.getPosts();

我不知道如何解决这个问题。请帮助它对我来说非常重要。如果您有更好的解决方案,欢迎。

说到这一行Post post = (Post) rec.getPosts(); ,LogCat 说

Cannot convert ArrayList to List

最佳答案

您误解了List<T>T并且代码的不同部分也存在同样的问题:

getPosts()返回List<Post>不是Post喜欢 getImages()返回List<Images>不是Images ,您可能需要一个循环来迭代您的 List<Post> ,单例Post然后获取其数据,如 List<Attachment> .

将其更改为以下内容:

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();
...
}
}

关于java - Android GSON访问ArrayList中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24259902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com