Gridview将不会更新Android

问题描述:

因此,我把一个图像里面的gridview,它工作正常时,开始和显示20图像,但当我尝试显示比它不会更新。 有一个条件,当gridview更新,当我加载20个图像时更新,但当我尝试加载更少或零时,它不会更新。Gridview将不会更新Android

我该如何解决?

,这里是我的适配器

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

没有错误?只是不更新​​? – Barns

+0

@ Barns52是的,只是没有更新 – mangkool

+0

你可以发布活动课吗? –

解决了,我必须把循环内的setAdapter方法并创建列表的新变量并使其成为最终的,不能使用全局变量。所以最终的代码应该看起来像这样

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