gpt4 book ai didi

java - 为什么我的自定义 ArrayAdapter 没有被填充?

转载 作者:行者123 更新时间:2023-12-01 21:15:57 27 4
gpt4 key购买 nike

数据通过 Retrofit 传输得很好,我的自定义适配器(NewsAdapter,基于 ArrayAdapter)之前已被填充,但我必须做出一些调整但我现在根本无法让它工作。

其想法是获取每个可用的新闻源,获取每个特定新闻源的文章,然后用这些文章填充我的 GridView。据我了解,NewsAdapter 上的 articles 成员被填充,但适配器本身没有被填充。这是我的代码和用于检查值的控制台输出:

控制台输出:

I/System.out: Source ID: nfl-news
I/System.out: Adapter count: 0
I/System.out: Response body: [pt.ismai.a26800.readr.Articles_Map@ca5fafc, pt.ismai.a26800.readr.Articles_Map@cd585, pt.ismai.a26800.readr.Articles_Map@8639fda, pt.ismai.a26800.readr.Articles_Map@9bbac0b, pt.ismai.a26800.readr.Articles_Map@ed5cae8, pt.ismai.a26800.readr.Articles_Map@c4a4501, pt.ismai.a26800.readr.Articles_Map@c6bdfa6, pt.ismai.a26800.readr.Articles_Map@876fde7, pt.ismai.a26800.readr.Articles_Map@3b0ad94, pt.ismai.a26800.readr.Articles_Map@b95303d]
I/System.out: Articles count: 10
I/System.out: Source ID: espn-cric-info
I/System.out: Adapter count: 0
I/System.out: Response body: [pt.ismai.a26800.readr.Articles_Map@f665032, pt.ismai.a26800.readr.Articles_Map@3ab9183, pt.ismai.a26800.readr.Articles_Map@4b50f00, pt.ismai.a26800.readr.Articles_Map@5a99339, pt.ismai.a26800.readr.Articles_Map@b253d7e, pt.ismai.a26800.readr.Articles_Map@50bc2df, pt.ismai.a26800.readr.Articles_Map@2dc1b2c]
I/System.out: Articles count: 17
I/System.out: Source ID: fox-sports
I/System.out: Adapter count: 0
I/System.out: Response body: [pt.ismai.a26800.readr.Articles_Map@80729f5, pt.ismai.a26800.readr.Articles_Map@253b38a, pt.ismai.a26800.readr.Articles_Map@513adfb, pt.ismai.a26800.readr.Articles_Map@385be18, pt.ismai.a26800.readr.Articles_Map@e6d7071]
I/System.out: Articles count: 22

列表新闻 Activity :

public class ListNewsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */

// parameters for Sources endpoint
String category = "sport";
String language = "en";
String country = "us";

// Sources endpoint
Sources_Interface client_sources = NewsAPI_Adapter.createService(Sources_Interface.class);
Call<Sources_Map> call_sources = client_sources.getData(category, language, country);

call_sources.enqueue(new Callback<Sources_Map>() {
@Override
public void onResponse(Call<Sources_Map> call_sources, Response<Sources_Map> response) {
if (response.body() != null) {
final NewsAdapter nAdapter = new NewsAdapter(ListNewsActivity.this,
R.layout.article_layout);

for (final Sources_Content source : response.body().sources) {
if (source.sortBysAvailable.contains("latest")) {

// Articles endpoint
NewsAPI_Interface client = NewsAPI_Adapter.createService(NewsAPI_Interface.class);
Call<NewsAPI_Map> call = client.getData(source.id, "apiKeyHere");

call.enqueue(new Callback<NewsAPI_Map>() {
@Override
public void onResponse(Call<NewsAPI_Map> call, Response<NewsAPI_Map> response) {
if (response.body() != null) {
ExpandableHeightGridView gv_content = (ExpandableHeightGridView) findViewById(R.id.gv_content);
nAdapter.addAll(response.body().articles);
System.out.println("Source ID: " + source.id + "\n" +
"Adapter count: " + nAdapter.getCount() + "\n" +
"Response body: " + response.body().articles + "\n" +
"Articles count: " + nAdapter.articles.size() + "\n");
gv_content.setAdapter(nAdapter);
gv_content.setExpanded(true);
}
}

@Override
public void onFailure(Call<NewsAPI_Map> call, Throwable t) {
System.out.println("An error ocurred!\n" +
"URL: " + call.request().url() + "\n" +
"Cause: " + t.getCause().toString());
}
});
}
}
}
}

@Override
public void onFailure(Call<Sources_Map> call_sources, Throwable t) {
System.out.println("An error ocurred!");
}
});
}
}

新闻适配器:

public class NewsAdapter extends ArrayAdapter<Articles_Map> {
Context mContext;
List<Articles_Map> articles;

public NewsAdapter(Context c, int resource) {
super(c, resource);
this.mContext = c;
this.articles = new ArrayList<>();
}

public NewsAdapter(Context c, int resource, List<Articles_Map> articles) {
super(c, resource, articles);
this.mContext = c;
this.articles = articles;
}

public void addAll(List<Articles_Map> articles) {
if (this.articles == null) {
this.articles = new ArrayList<>(articles);
} else {
this.articles.addAll(articles);
}
notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the property we are displaying
Articles_Map article = articles.get(position);

// get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.article_layout, null);

ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
TextView title = (TextView) view.findViewById(R.id.title);
TextView description = (TextView) view.findViewById(R.id.description);

Picasso.with(mContext).load(article.urlToImage).into(thumbnail);
title.setText(article.title);
description.setText(article.description);

return view;
}
}

NewsAPI_Map:

public class NewsAPI_Map {
String status;
String source;
List<Articles_Map> articles;

public NewsAPI_Map(String status, String source, List<Articles_Map> articles) {
this.status = status;
this.source = source;
this.articles = articles;
}
}

文章 map :

public class Articles_Map {
String title;
String description;
String url;
String urlToImage;

public Articles_Map(String title, String description, String url, String urlToImage) {
this.title = title;
this.description = description;
this.url = url;
this.urlToImage = urlToImage;
}
}

最佳答案

尝试插入 getCount() 方法:

@Override
public int getCount(){
int size = articles == null ? 0 : articles.size();
return size;
}

关于java - 为什么我的自定义 ArrayAdapter 没有被填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096433/

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