0

When my device is online, the RecyclerView adapter works as expected. When offline, onCreateViewHolder is not called.

First off, I am aware of this: Recyclerview not call onCreateViewHolder and it is not the problem. getItemCount() returns a number >0 and I have set the order of my adapter and layout manager correctly in the calling activity:

mAdapter = new MovieAdapter(this, this, movies, mScreenWidthPx, mIsFavorites);
    mMovieRecyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
    mMovieRecyclerView.setAdapter(mAdapter);

So, it seems to be related to the online/offline state, but I cannot figure out how. Here is the adapter:

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

private static final String TAG = MovieAdapter.class.getSimpleName();

private ArrayList<Movie> mMovies;
private Context mContext;
private MovieClickListener mClickListener;
private int mScreenWidthPx;
private boolean mIsFavorite;

public interface MovieClickListener {
    void onMovieClick(Movie movie);
}

public MovieAdapter(Context context, MovieClickListener movieClickListener, ArrayList<Movie> movies, int screenWidthPx, boolean isFavorite) {
    Log.d(TAG,"creating MovieAdapter");
    this.mContext = context;
    this.mMovies = movies;
    this.mClickListener = movieClickListener;
    this.mScreenWidthPx = screenWidthPx;
    this.mIsFavorite = isFavorite;
    Log.d(TAG,"mMovies size constructor:" + mMovies.size());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.d(TAG,"onCreateViewHolder");
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_movie, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Log.d(TAG,"onBindViewHolder");
    if (mIsFavorite) {
        Log.d(TAG,"loading local image file");
        String fileName = ImageUtils.getMoviePosterFileName(mMovies.get(position).getTitle());
        File imageFile = ImageUtils.getImageFile(mContext,fileName);
        Picasso.with(mContext)
                .load(imageFile)
                .into(holder.mImagePoster);
    }
    else
        {
        Log.d(TAG,"loading remote image file");
        String posterUrl = NetworkUtils.buildMoviePosterUrl(mMovies.get(position).getPosterPath(), mScreenWidthPx);
        Picasso.with(mContext)
                .load(posterUrl)
                .into(holder.mImagePoster);
    }
}

@Override
public int getItemCount() {
    Log.d(TAG,"mMovies size getCount:" + mMovies.size());
    return mMovies.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    @BindView(R.id.image_movie_poster) ImageView mImagePoster;

    ViewHolder(View itemView) {
        super(itemView);
        Log.d(TAG,"creating ViewHolder");
        ButterKnife.bind(this, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int adapterPosition = getAdapterPosition();
        mClickListener.onMovieClick(mMovies.get(adapterPosition));
    }
}
}
fullmeriffic
  • 236
  • 2
  • 14

2 Answers2

1

None of this has to do with network which tells me that your array that is passed in called movies is probably being altered from another class when your network state changes. Good luck.

Tony
  • 4,609
  • 2
  • 22
  • 32
0

In offline mode Picasso library not able to download the image and Image result will null in this case. You might need to set the NetworkPolicy for Picasso, Something like below:- If there is a network or not try to download the image from cache first and if it fails then download from the network . Here is the code snippet:-

Picasso.with(context)
                    .load(Uri.parse(getItem(position).getStoryBigThumbUrl()))
                    .networkPolicy(NetworkPolicy.OFFLINE)
                    .into(holder.storyBigThumb, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {
                            // Try again online if cache failed
                            Picasso.with(context)
                                    .load(Uri.parse(getItem(position)
                                            .getStoryBigThumbUrl()))
                            .placeholder(R.drawable.user_placeholder)
                            .error(R.drawable.user_placeholder_error)
                                    .into(holder.storyBigThumb);
                        }
                    });
lib4backer
  • 3,337
  • 3
  • 18
  • 16