Himanshu Babal Himanshu Babal - 4 months ago 57
Android Question

RecyclerView Adapter Methods not being called

My RecyclerView's Adapter's methods are not being called.


  • Only
    getItemCount()
    and class Constructor are called (List is not
    empty, i have checked that).

  • Methods
    onCreateViewHolder()
    and
    onBindViewHolder()
    are not called.

  • I have Other fragments in the same activity, using nearly same
    procedure but they are working perfectly fine.

  • I tried creating a new Adapter class but that also didn't work.

  • Method
    getItemCount()
    is called 2 times

  • I am using same XML file for Adapter View for 2 different recyclerViews and they both are working fine.



My Adapter Class

public class OfflineAdapter extends RecyclerView.Adapter<OfflineAdapter.OfflineHolder>{
private Context context;
private List<MovieDBObject> list;

public class OfflineHolder extends RecyclerView.ViewHolder {
@BindView(R.id.movie_grid_imageView)
ImageView imageView;

public OfflineHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
Log.i("bitmap", String.valueOf(4));
}
}

public OfflineAdapter (Context context, List<MovieDBObject> list) {
this.context = context;
this.list = list;
Log.i("bitmap", String.valueOf(1));
}

@Override
public OfflineHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_grid_imageview, parent, false);
Log.i("bitmap", String.valueOf(3));
return new OfflineHolder(view);
}

@Override
public void onBindViewHolder(OfflineHolder holder, int position) {
MovieDBObject object = list.get(position);
holder.imageView.setImageBitmap(object.getPoster());
Log.i("bitmap", String.valueOf(5));
}

@Override
public int getItemCount() {
Log.i("bitmap", String.valueOf(2));
return list.size();
}
}


Fragment Calling RecyclerView

public class FavouriteMovies extends Fragment{
@BindView(R.id.favourite_movies_recyclerView)
RecyclerView recyclerView;
List<MovieDBObject> list = new ArrayList<>();
MovieDBHelper movieDBHelper;
OfflineAdapter offlineAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fav_movies, container, false);
ButterKnife.bind(this, rootView);

movieDBHelper = ((MainActivity)getActivity()).dbHelper;
final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);

offlineAdapter = new OfflineAdapter(getContext(), list);
recyclerView.setAdapter(offlineAdapter);
recyclerView.setLayoutManager(layoutManager);
recyclerView.hasFixedSize();
recyclerView.setItemAnimator(new DefaultItemAnimator());
prepareList();
return rootView;
}

private void prepareList(){
list = movieDBHelper.getAllMovies();
Log.i("bitmap", list.size()+"");
offlineAdapter.notifyDataSetChanged();
}
}


XML File for the Fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"/>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/favourite_movies_recyclerView"/>

</LinearLayout>


XML File for the AdapterView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/movie_grid_imageView"/>

</RelativeLayout>

Answer

In OfflineAdapter create a new method to set update data example:

private void updateData(List<MovieDBObject> dataList){
    this.list = dataList;
}

then update prepareList

private void prepareList(){
    list = movieDBHelper.getAllMovies();
    Log.i("bitmap", list.size()+"");
    offlineAdapter.updateData(list);
    offlineAdapter.notifyDataSetChanged();
}

That will fix your problem

Comments