Search code examples
androidgridviewandroid-adaptercustom-adapterandroid-recyclerview

Custom Adapter not populating GridView


I have made a custom adapter to populate my gridView. What I am doing is that i am trying to populate the gridview with two different data(that too are custom) which are: Movies and FavouriteMovies. when the dridview is updated using Movies it updates just fine but when it FavouriteMovies it does not. I am storing FavouriteMovies in a sqlite db and then querying it to return a cursor.the data returns perfectly and there is no error. the main problem is with my MoviesAdapter. inside this when i add Movies to adapter it adds to the list inside MoviesAdapter and it works perfectly but when I add FavouriteMovies to the adapter it does not add it inside the favouriteList inside MoviesAdapter and its size is always zero. please help!! here is my MoviesAdapter: package com.akshitjain.popularmovies;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


public class MoviesAdapter extends BaseAdapter {


private Context mContext;
List<Movies> list = new ArrayList<>();
List<FavouriteMovies> favouriteList = new ArrayList<>();

public MoviesAdapter(Context c) {
    this.mContext = c;
}

@Override
public int getCount() {
    if (list != null)
        return list.size();
    else
        return favouriteList.size();
}

@Override
public Object getItem(int position) {
    if (list != null)
        return list.get(position);
    else
        return favouriteList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView =     LayoutInflater.from(mContext).inflate(R.layout.grid_item_movies, parent, false);
    }

    ImageView moviePoster = (ImageView) convertView.findViewById(R.id.grid_item_movie_image);
    if (list != null) {
        Movies movies = list.get(position);
        String posterPath = movies.posterPath;
        final String POSTER_FINAL_URL = Constants.IMAGE_BASE_URL +     Constants.POSTER_SIZE_LARGE + posterPath;

            Picasso.with(mContext).load(POSTER_FINAL_URL.trim()).into(moviePoster);
    } else {
        FavouriteMovies favouriteMovies = favouriteList.get(position);
        byte[] bb = favouriteMovies.posterImage;
        moviePoster.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0,       bb.length));
    }

    return convertView;
}

public void add(Movies movies) {
    list.add(movies);
}

public void offlineAdd(FavouriteMovies favouriteMovies) {
    favouriteList.add(favouriteMovies);
}

public void clear() {
    list.clear();
}

}

and here is where I am adding to the adapter:

          @Override
    protected void     onPostExecute(Movies[] strings) {
    if (strings != null) {
        mMoviesAdapter.clear();
        for (Movies moviesStr : strings) {
            mMoviesAdapter.add(moviesStr);
        }
    }else if(mFavouriteMovies != null){
        mMoviesAdapter.clear();
        for(FavouriteMovies     favouriteMoviesStr : mFavouriteMovies){
              mMoviesAdapter.offlineAdd(favouriteMoviesStr);
        }
    }
    Log.v("FetchMoviesTask","Count :" +     mMoviesAdapter.getItem(0));
        mMoviesAdapter.notifyDataSetChanged();
}

So sorry for my crude language.


Solution

  • You will always have count == 0 if you set your favorite movies.

    List<Movies> list = new ArrayList<>();
    
    @Override
    public int getCount() {
        if (list != null)
            return list.size();
        else
            return favouriteList.size();
    }
    

    This will always return a count of 0 since list never is null. You favorite movies get ignored.

    Solutions

    • Since you are only displaying either movies or favorites you should use 2 adapters. If you don't want to do this,
    • Use a boolean. Just set showFavorites to true, and display those. Or if you keep your null checks,
    • set the List<Movie> = null; when displaying favorited, then it will also work.