为什么我的arraylist只显示一项?

时间:2019-05-04 17:05:16

标签: java

我有一个凌空JsonArrayRequest,用它用数据填充数组列表。当我将对象打印到日志中时,我得到了所需的结果,但是在将该项目添加到我的数组列表中之后,我只得到了一个应该不是的项目。

这是我的代码

private void upcomingEpisodes() {

    list = new ArrayList<>();

    getUpcomingEpisodes(new DataCallback() {
        @Override
        public void onSuccess(JSONArray result) {

            try {

                for (int i = 0; i < result.length(); i++) {

                    JSONObject mainObject = result.getJSONObject(i);
                    JSONObject episodeObject = mainObject.getJSONObject("episode");
                    JSONObject showObject = mainObject.getJSONObject("show");
                    JSONObject showIdObject = showObject.getJSONObject("ids");

                    String title = showObject.getString("title");
                    Log.i("vkv", title);

                    ShowsArraylists showsArraylists = new ShowsArraylists();
                    showsArraylists.setAir_date(mainObject.getString("first_aired"));
                    showsArraylists.setSeason_number(episodeObject.getInt("season"));
                    showsArraylists.setEpisode_number(episodeObject.getInt("number"));
                    String episodeTitle = episodeObject.optString("title").replace("null", "string is null");
                    showsArraylists.setEpisode_title(episodeTitle);
                    showsArraylists.setTitle(showObject.getString("title"));
                    showsArraylists.setTmdbId(showIdObject.getString("tmdb"));
                    showsArraylists.setTraktId(showObject.getString("trakt"));

                    list.add(showsArraylists);
                }

                UpcomingShowsAdapter.RecyclerViewClickListener listener = new UpcomingShowsAdapter.RecyclerViewClickListener() {
                    @Override
                    public void onItemClick(ShowsArraylists showsArraylists) {
                        Toast.makeText(getActivity(), "Item clicked", Toast.LENGTH_LONG).show();
                    }
                };

                adapter = new UpcomingShowsAdapter(getActivity(), list, listener);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                progressBar.setVisibility(View.GONE);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String error) {
            Log.i("vkv", "getUpcomingShows(): " + error);
        }
    });
}

在这里登录后我得到了必要的结果

String title = showObject.getString("title");
                    Log.i("vkv", title);

但是添加此问题后问题开始出现

ShowsArraylists showsArraylists = new ShowsArraylists();
                    showsArraylists.setAir_date(mainObject.getString("first_aired"));
                    showsArraylists.setSeason_number(episodeObject.getInt("season"));
                    showsArraylists.setEpisode_number(episodeObject.getInt("number"));
                    String episodeTitle = episodeObject.optString("title").replace("null", "string is null");
                    showsArraylists.setEpisode_title(episodeTitle);
                    showsArraylists.setTitle(showObject.getString("title"));
                    showsArraylists.setTmdbId(showIdObject.getString("tmdb"));
                    showsArraylists.setTraktId(showObject.getString("trakt"));

                    list.add(showsArraylists);

编辑

我添加了ShowsArraylists

public class ShowsArraylists implements Serializable {

private String title;
private String traktId;
private String tmdbId;
private String air_date;
private int episode_number;
private String episode_title;
private int season_number;
private String episode_overview;

public String getAir_date() {
    return air_date;
}

public void setAir_date(String air_date) {
    this.air_date = air_date;
}

public int getEpisode_number() {
    return episode_number;
}

public void setEpisode_number(int episode_number) {
    this.episode_number = episode_number;
}

public String getEpisode_title() {
    return episode_title;
}

public void setEpisode_title(String episode_title) {
    this.episode_title = episode_title;
}

public int getSeason_number() {
    return season_number;
}

public void setSeason_number(int season_number) {
    this.season_number = season_number;
}

public String getEpisode_overview() {
    return episode_overview;
}

public void setEpisode_overview(String episode_overview) {
    this.episode_overview = episode_overview;
}

public String getTmdbId() {
    return tmdbId;
}

public void setTmdbId(String tmdbId) {
    this.tmdbId = tmdbId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getTraktId() {
    return traktId;
}

public void setTraktId(String traktId) {
    this.traktId = traktId;
}

}

0 个答案:

没有答案