2017-08-05 19 views
0

私はgridviewの内部に画像を置くので、最初と20枚の画像を表示するとうまく動作しますが、それ以下の画像を表示しようとすると更新されません。 グリッドビューが更新されると、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の新しい変数を作成して最終的に作成します。グローバルなものを使用することはできません。最終的なコードは次のようになります

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(); 
      } 
     }); 
    } 
関連する問題