gpt4 book ai didi

java - 无法使用Retrofit在ListView中显示数据

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

我在 ListView 中显示数据时遇到问题。我从 Retrofit 响应中获取数据,但我的 Activity (应该显示这些数据)始终是空白的。我确信我已经收到数据,我已经在控制台中检查了它。

模型类

public class itemList_model {
@SerializedName("results")
private List<itemList_Results> results;

public List<itemList_Results> getResults() {
return results;
}

public static class itemList_Results{
@SerializedName("title")
String title;
@SerializedName("vote_average")
Double vote;
@SerializedName("genre_ids")
List<Integer> genresId;
@SerializedName("release_date")
String releaseDate;

public itemList_Results(String title, Double vote, String releaseDate) {
this.title = title;
this.vote = vote;
this.releaseDate = releaseDate;
}

public String getTitle() {
return title;
}

public Double getVote() {
return vote;
}

public List<Integer> getGenresId() {
return genresId;
}

public String getReleaseDate() {
return releaseDate;
}
}

public class itemList_genresId{
@SerializedName("genre_ids")
int id;

public int getId() {
return id;
}
}
}

适配器类

public class genres_adapter extends ArrayAdapter<itemList_model.itemList_Results> {
RetrofitCalls calls;

public genres_adapter(@NonNull Context context, ArrayList<itemList_model.itemList_Results> list) {
super(context, 0, list);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View itemView = convertView;
if (itemView == null){
itemView = LayoutInflater.from(getContext()).inflate(R.layout.genres_item_view, parent, false);
}
itemList_model.itemList_Results model = getItem(position);

TextView title = itemView.findViewById(R.id.title);
title.setText(model.getTitle());
TextView vote = itemView.findViewById(R.id.vote);
vote.setText(String.valueOf(model.getVote()));
TextView release = itemView.findViewById(R.id.release);
release.setText(model.getReleaseDate());
return itemView;
}
}

Activity java类

ArrayList<itemList_model.itemList_Results> arrayList;
genres_adapter adapter;

String title = "";
Double average_votes = 0.0;
String date = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_genres_list_view);

arrayList = new ArrayList<itemList_model.itemList_Results>();
adapter = new genres_adapter(this, arrayList);

ListView listView = (ListView) findViewById(R.id.genres_listView);
listView.setAdapter(adapter);
getListViewItems();
}
public void getListViewItems(){

String url = "https://api.themoviedb.org/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();

apiCall api = retrofit.create(apiCall.class);
Call<itemList_model> call = api.getHorror();
call.enqueue(new Callback<itemList_model>() {
@Override
public void onResponse(Call<itemList_model> call, Response<itemList_model> response) {
if (!response.isSuccessful()) {
Log.i(TAG, "onResponse: " + response.code());
}
List<itemList_model.itemList_Results> list = response.body().getResults();
for (itemList_model.itemList_Results model : list){

title =model.getTitle();
average_votes = Double.valueOf(model.getVote());
date =model.getReleaseDate();
}
list.add(new itemList_model.itemList_Results(title,average_votes,date));
}
@Override
public void onFailure(Call<itemList_model> call, Throwable t) {
Log.i(TAG, "onFailure: "+t.getMessage());
}
});
}
  • 包含 ListView 的 Activity 称为 Activity_genres_list_view
  • 适配器将使用的 Activity 称为genrenes_item_view

我猜,这是关于列表中的数据,也许它没有被添加?

最佳答案

显然,您应该在收到列表后将其添加到适配器中:

adapter.addAll(list);

关于java - 无法使用Retrofit在ListView中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567189/

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