简体   繁体   English

无法使用改造在ListView中显示数据

[英]Can't display data in ListView using Retrofit

I 've problem with display data in ListView. 我在ListView中显示数据有问题。 I get data from Retrofit response, but my activity, which should display this data, is just beeing blank, all the time. 我从Retrofit响应中获取数据,但是我的活动(应该显示此数据)一直都是空白。 I am sure, that I've receiving data, I've checked it, in console. 我敢肯定,我已经在控制台中收到了数据,并检查了它。

Model class 模型类

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

adapter class 适配器类

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 class 活动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());
        }
    });
}
  • activity, that contains ListView, is called activity_genres_list_view 包含ListView的活动称为activity_genres_list_view
  • activity which will be used by adapter, is called genres_item_view 适配器将使用的活动称为genres_item_view

I'm guess, it's about data from list, maybe it's not being added? 我猜是关于列表中的数据,也许没有添加?

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

adapter.addAll(list);

I cannot see any notifyDataSetChanged() call in your code. 我在您的代码notifyDataSetChanged()不到任何notifyDataSetChanged()调用。 When the response is found from the Retrofit call, you need to update the list which was passed to the adapter and call notifyDataSetChanged on your adapter to see the effect. Retrofit调用中找到响应后,您需要更新传递给适配器的列表,并在适配器上调用notifyDataSetChanged以查看效果。

Hence you might consider doing something like the following. 因此,您可以考虑执行以下操作。

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

            // Do not create a new list here. Use the arrayList which was declared before and passed to the adapter
            // List<itemList_model.itemList_Results> list = response.body().getResults();

            // Clear the arrayList before pushing new data
            arrayList.clear();

            for (itemList_model.itemList_Results model : list){
                title =model.getTitle();
                average_votes = Double.valueOf(model.getVote());
                date = model.getReleaseDate();

                // Add the data into the arrayList instead of the list
                arrayList.add(new itemList_model.itemList_Results(title,average_votes,date));
            }

            // Now call notifyDataSetChanged on your adapter to see the changes in the ListView
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<itemList_model> call, Throwable t) {
            Log.i(TAG, "onFailure: "+t.getMessage());
        }
    });
}

Hope that fixes your problem. 希望能解决您的问题。

Nothing appear because you are setting adapter before getting information : 什么都没有出现,因为您在获取信息之前正在设置适配器:

    adapter = new genres_adapter(this, arrayList);//array list is still empty here
    listView.setAdapter(adapter);
    getListViewItems();

what yo should do is setting adapter once you get the list ! 您应该做的就是在获得列表后设置适配器! or call notifydatasetChanged on your adapter after you get your informations ! 或在获取信息后在适配器上调用notifydatasetChanged

{
.....
arrayList.add(new itemList_model.itemList_Results(title,average_votes,date));
.....
}
adapter.notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM