2017-08-05 127 views
0

因此,我把一個圖像裏面的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

你可以發佈活動課嗎? –

回答

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