我称热门电影的API(我需要海报和标题)和热门电视节目(海报和标题)。我需要通过2个不同的异步调用来完成,将数据放入List<PopularMovieAdapterObject> podaci
和setAdapter
。适配器用于GridView
,所以基本上我需要在一个gridview中混合流行电影和流行电视节目。这是我的代码:
PopularMoviesFromAPI popularMoviesFromAPI = new PopularMoviesFromAPI();
popularMoviesFromAPI.getPopularMoviesInfo(new PopularMoviesFromAPI.GenericCallback() {
@Override
public void success(@NonNull List<String> posterURL, List<String> movieTVName) {
List<PopularMovieAdapterObject> podaci = new ArrayList<>();
for(int i=0; i<(posterURL.size()/2); i++){
podaci.add(new PopularMovieAdapterObject("https://image.tmdb.org/t/p/w500" + posterURL.get(i), movieTVName.get(i)));
}
PopularMoviesAdapter popularMoviesAdapter = new PopularMoviesAdapter(getContext(), R.layout.popular_movies_item, podaci);
popularMoviesGridView.setAdapter(popularMoviesAdapter);
}
@Override
public void failure(@NonNull Throwable throwable) {
}
});
popularMoviesFromAPI.getPopularTVShowsInfo(new PopularMoviesFromAPI.GenericCallback() {
@Override
public void success(@NonNull List<String> posterURL, List<String> movieTVName) {
List<PopularMovieAdapterObject> podaci = new ArrayList<>();
for(int i=0; i<(posterURL.size()/2); i++){
podaci.add(new PopularMovieAdapterObject("https://image.tmdb.org/t/p/w500" + posterURL.get(i), movieTVName.get(i)));
}
PopularMoviesAdapter popularMoviesAdapter = new PopularMoviesAdapter(getContext(), R.layout.popular_movies_item, podaci);
popularMoviesGridView.setAdapter(popularMoviesAdapter);
}
@Override
public void failure(@NonNull Throwable throwable) {
}
});
现在我可以在GridView
放置电影或电视节目。
答案 0 :(得分:1)
这种情况正在发生,因为您正在设置适配器两次。
尝试:
未将List(podaci)作为参数传递给适配器类;
在适配器的构造函数中,最初将列表设置为空列表;
创建一个方法,通过在现有列表的末尾逐个添加podaci元素来设置适配器列表。