【发布时间】:2019-08-21 16:36:29
【问题描述】:
我正在开发体育新闻安卓应用,但是当我运行应用时它显示白屏screenshot of emulator。
在我的适配器类下面
公共类 ArticleAdapter 扩展 RecyclerView.Adapter {
private List<Article> articles;
public ArticleAdapter(List<Article> articles, SportNews sportNews) {
this.articles = articles;
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = (View) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.article_list, null);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int position) {
Article article = articles.get(position);
customViewHolder.articleAuthor.setText(article.getAuthor());
customViewHolder.articleTitle.setText(article.getTitle());
Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);
}
@Override
public int getItemCount() {
if(articles == null) return 0;
return articles.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.articleAuthor)
TextView articleAuthor;
@BindView(R.id.articleTitle)
TextView articleTitle;
@BindView(R.id.articleImage)
ImageView articleImage;
public CustomViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
下面的改造接口类 公共接口 SportInterface {
@GET("/v2/top-headlines?sources=bbc-sport&apiKey=d03441ae1be44f9cad8c38a2fa6db215")
Call<SportNews> getArticles();
}
改造客户端下方
公共类 SportClient {
private static final String ROOT_URL = "https://newsapi.org";
/**
* Get Retrofit Instance
*/
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
/**
* Get API Service
*
* @return API Service
*/
public static SportInterface getApiService() {
return getRetrofitInstance().create(SportInterface.class);
}
}
在 MainActivity.java 下
公共类 MainActivity 扩展 AppCompatActivity {
private SportNews sportNews;
private List<Article> articleList;
private ArticleAdapter articleAdapter;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
SportInterface sportInterface = SportClient.getApiService();
Call<SportNews> call = sportInterface.getArticles();
call.enqueue(new Callback<SportNews>() {
@Override
public void onResponse(Call<SportNews> call, Response<SportNews> response) {
sportNews = response.body();
articleAdapter = new ArticleAdapter( articleList, sportNews);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(articleAdapter);
}
@Override
public void onFailure(Call<SportNews> call, Throwable t) {
}
});
}
}
低于 Model SportNews 类
公开课体育新闻{
@SerializedName("status")
@Expose
private String status;
@SerializedName("totalResults")
@Expose
private Integer totalResults;
@SerializedName("articles")
@Expose
private List<Article> articles = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getTotalResults() {
return totalResults;
}
public void setTotalResults(Integer totalResults) {
this.totalResults = totalResults;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
}
下面的json响应
{
"status": "ok",
"totalResults": 10,
"articles": [
{
"source": {
"id": "bbc-sport",
"name": "BBC Sport"
},
"author": "BBC Sport",
"title": "Gordon Taylor: PFA chief executive set to step down after 38 years",
"description": "Gordon Taylor to announce he will step down as chief executive of the Professional Footballers' Association (PFA) at the end of the season after 38 years.",
"url": "http://www.bbc.co.uk/sport/football/47691299",
"urlToImage": "https://ichef.bbci.co.uk/onesport/cps/624/cpsprodpb/13590/production/_97584297_breaking_news.png",
"publishedAt": "2019-03-27T13:04:18Z",
"content": "Gordon Taylor is set to announce he is standing down as chief executive of the Professional Footballers' Association after 38 years in the role.\r\nIt follows a mutiny from PFA chairman Ben Purkiss and former players over governance issues and controversy over … [+675 chars]"
},
]
}
【问题讨论】:
-
登录
onFailure并检查您是否收到任何东西 -
@HB 你还有什么建议
-
其他建议是什么意思?你检查
onFailure了吗?
标签: java android android-recyclerview retrofit2