所以我在gridview中放置了一个图像,它在开始时工作正常,并显示20个图像,但是当我试图显示少于它时,它不会更新。 当gridview更新时有一个条件,当我加载20个图像时它会更新,但是当我尝试加载更少或零时它不会更新。
我该如何解决这个问题?
这是我在适配器上的代码
private final Context context;
private List<Movie> urls = new ArrayList<>();
public MovieGridViewAdapter(Context context, List<Movie> urls) {
this.context = context;
this.urls = urls;
}
@Override
public int getCount() {
if (urls.size() == 0){
return 0;
}
return urls.size();
}
@Override
public Movie getItem(int position) {
return urls.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View gridView = view;
if (gridView == null) {
gridView = LayoutInflater.from(context)
.inflate(R.layout.item_poster, viewGroup, false);
}
ImageView posterImageView = (ImageView) gridView.findViewById(R.id.posterImageView);
// Get the image URL for the current position.
Movie movie = getItem(position);
//needed to append the image url
String imageBaseUrl = "http://image.tmdb.org/t/p/w185";
Picasso.with(context) //
.load(imageBaseUrl+movie.getPosterPath()) //
.placeholder(R.drawable.ic_hourglass_empty_black_24dp) //
.error(R.drawable.ic_error_black_24dp) //
.fit() //
.tag(context) //
.into(posterImageView);
Log.v("jalan ji", "jalan");
return gridView;
}
这是我尝试更新Gridview的地方
List<Favorite> favorites = new ArrayList<>();
Cursor cursor = getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI,
null,
null,
null,
MovieContract.MovieEntry.COLUMN_TITLE);
while(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_MOVIE_ID));
String title = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_TITLE));
try{
Favorite fav = new Favorite();
fav.setId(id);
fav.setTitle(title);
favorites.add(fav);
}catch (Exception e){
e.printStackTrace();
}
}
for(Favorite favorite : favorites){
Call<MovieSingle> call = movieDbClient.getMovie(favorite.getId(), apiKey);
setTitle("Favorite Movies");
call.enqueue(new Callback<MovieSingle>() {
@Override
public void onResponse(@NonNull Call<MovieSingle> call, @NonNull Response<MovieSingle> response) {
Movie mov = new Movie();
mov.setBackdropPath(response.body().getBackdrop_path());
mov.setOverview(response.body().getOverview());
mov.setReleaseDate(response.body().getRelease_date());
mov.setTitle(response.body().getTitle());
mov.setVoteAverage(response.body().getVote_average());
mov.setPosterPath(response.body().getPoster_path());
movie.add(mov);
Log.v("berhasil", " "+response.body().getTitle());
}
@Override
public void onFailure(@NonNull Call<MovieSingle> call, @NonNull Throwable t) {
t.printStackTrace();
pbar.setVisibility(View.INVISIBLE);
Log.v("gagal", "berhasil");
showErrorMessage();
}
});
}
showGridView();
pbar.setVisibility(View.INVISIBLE);
MovieGridViewAdapter movieGridViewAdapter = new MovieGridViewAdapter(getApplicationContext(), movie);
Log.v("Test", movie.get(2).getTitle());
Log.v("Test", movie.get(2).getPosterPath());
movieGridViewAdapter.notifyDataSetChanged();
gridView.invalidateViews();
gridView.setAdapter(movieGridViewAdapter);
和布局
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgdetail">
<GridView android:id="@+id/movieitem_grid"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="165dp"
android:scrollbarStyle="insideOverlay"
android:scrollbars="none"
android:listSelector="@null"
android:numColumns="auto_fit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_error"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="@color/textColor"
android:text="@string/error_msg"
android:visibility="invisible"
android:textSize="22sp"/>
<ProgressBar
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_gravity="center"
android:id="@+id/progressbar"
android:visibility="invisible"/>
</FrameLayout>
答案 0 :(得分:0)
解决了,我必须将setAdapter方法放在循环中并创建list的新变量并使其成为final,不能使用全局变量。所以最终的代码看起来应该是这样的
List<Favorite> favorites = new ArrayList<>();
final List<Movie> movie1 = new ArrayList<>();
Cursor cursor = getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI,
null,
null,
null,
MovieContract.MovieEntry.COLUMN_TITLE);
while(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_MOVIE_ID));
String title = cursor.getString(cursor.getColumnIndex(MovieContract.MovieEntry.COLUMN_TITLE));
try{
Favorite fav = new Favorite();
fav.setId(id);
fav.setTitle(title);
favorites.add(fav);
}catch (Exception e){
e.printStackTrace();
}
}
for(Favorite favorite : favorites){
Call<MovieSingle> call = movieDbClient.getMovie(favorite.getId(), apiKey);
setTitle("Favorite Movies");
call.enqueue(new Callback<MovieSingle>() {
@Override
public void onResponse(@NonNull Call<MovieSingle> call, @NonNull Response<MovieSingle> response) {
Movie mov = new Movie();
mov.setBackdropPath(response.body().getBackdrop_path());
mov.setOverview(response.body().getOverview());
mov.setReleaseDate(response.body().getRelease_date());
mov.setTitle(response.body().getTitle());
mov.setVoteAverage(response.body().getVote_average());
mov.setPosterPath(response.body().getPoster_path());
movie1.add(mov);
showGridView();
pbar.setVisibility(View.INVISIBLE);
MovieGridViewAdapter movieGridViewAdapter = new MovieGridViewAdapter(getApplicationContext(), movie1);
movieGridViewAdapter.notifyDataSetChanged();
gridView.setAdapter(movieGridViewAdapter);
Log.v("berhasil", " "+movie1.get(0).getTitle());
}
@Override
public void onFailure(@NonNull Call<MovieSingle> call, @NonNull Throwable t) {
t.printStackTrace();
pbar.setVisibility(View.INVISIBLE);
Log.v("gagal", "berhasil");
showErrorMessage();
}
});
}