我在Android的“回收视图”中显示数据时遇到问题。数据来自一个API,该API在我单独测试时可以正常工作。我已经实现了DTO模式,因为数据位于主JSON内的数据JSON内。 我正在使用Retrofit从API获取数据。 我将在下面保留代码
ArticleActivity.Java
public class ArticleActivity extends AppCompatActivity {
private final String baseUrl = "http://127.0.0.1:3000/";
List<Article> listaArticles = new ArrayList<>();
ArticleAdapter adapter;
RecyclerView rvClientes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
rvClientes = (RecyclerView) findViewById(R.id.rvClientes); // change this ID
Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
rvClientes.setLayoutManager(llm);
adapter = new ArticleAdapter(listaArticles);
rvClientes.setAdapter(adapter);
DividerItemDecoration itemDecoration = new DividerItemDecoration(rvClientes.getContext(),llm.getOrientation());
rvClientes.addItemDecoration(itemDecoration);
ArticleService articleService = retrofit.create(ArticleService.class);
Call<ArticlesDTO> callArticleDTO = articleService.getArticles();
callArticleDTO.enqueue(new Callback<ArticlesDTO>() {
@Override
public void onResponse(Call<ArticlesDTO> call, Response<ArticlesDTO> response) {
if(response.isSuccessful()) {
listaArticles = response.body().data;
adapter = new ArticleAdapter(listaArticles);
rvClientes.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ArticlesDTO> call, Throwable t) {
}
});
}
}
ArticleAdapter.Java
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {
List<Article> lista;
public ArticleAdapter(List<Article> lista) {
this.lista = lista;
}
@Override
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_row,parent,false);
return new ArticleViewHolder(v);
}
@Override
public void onBindViewHolder(ArticleViewHolder holder, int position) {
holder.bindArticle(lista.get(position));
}
@Override
public int getItemCount() {
return lista.size();
}
public class ArticleViewHolder extends RecyclerView.ViewHolder {
TextView tvNombre;
TextView tvId;
public ArticleViewHolder(View itemView) {
super(itemView);
tvNombre = (TextView) itemView.findViewById(R.id.tvArticle);
tvId = (TextView) itemView.findViewById(R.id.tvID);
}
public void bindArticle(Article article) {
String nombre = article.getTitle() + ":";
tvNombre.setText(nombre);
tvId.setText(String.valueOf(article.getId()));
}
}
}
ArticlesDTO
public class ArticlesDTO {
public String status;
public String message;
public List<Article> data;
}
ArticleService
public interface ArticleService {
@GET("api/v1/articles")
Call<ArticlesDTO> getArticles();
}
问题是,当我打开该应用程序时,它只显示黑屏,没有任何错误。我使用断点检查数组列表的大小,它为零
答案 0 :(得分:2)
每当从API获得响应时,您都将设置一个新适配器
//here is the problem
if (response.isSuccessful()) {
listaArticles = response.body().data;
adapter = new ArticleAdapter(listaArticles);
rvClientes.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
代替使用现有适配器,然后将数据添加到适配器
public void addAll(List<Article> data){
this.lista = data;
notifyDataSetChanged();
}
,然后当您获得回复
if (response.isSuccessful()) {
listaArticles = response.body().data;
adapter.addAll(listaArticles);
}
答案 1 :(得分:1)
似乎您输入了错误的网址(http://127.0.0.1:3000/) 这是一个localhost网址,不能用于android设备。 尝试在云上部署后端服务,并为android应用提供适当的基本URL。