RecyclerView.Adapter's onCreateViewHolder and onBindViewHolder methods are not getting called

Prudhvi picture Prudhvi · Sep 8, 2015 · Viewed 8.4k times · Source

I'm trying to implement TwoWayView in my project by following the sample project provided in that link. I have implemented everything and my code works without any errors but the actual List is not getting inflated. Upon debugging I found out that the adapter is set correctly and the getItemCount method is also returning positive count but the other two overridden methods onCreateViewHolder and onBindViewHolder are not getting called. I have no clue why it is not working. Please see the partial code below, any help is appreciated. Thanks.

MyAdapter.java class

public class MyAdapter extends RecyclerView.Adapter < MyViewHolder > {

    private MySimpleCursorAdapter mCursor;
    //some other private fields here

    public MyAdapter(Context context, Cursor c, int resourceId, ViewType viewType, String containerId) {
        mCursor = new MySimpleCursorAdapter(a1,a2,a3,a4,a5,a6); //Passed necessary arguments
        ....
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.w("onCreateViewHolder", "--------->executed"); //Line not executed
        final View view = mCursor.newView(mContext, mCursor.getCursor(), parent);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.w("onBindViewHolder", "--------->executed"); //Line not executed
        mCursor.bindView(holder.itemView, mContext, mCursor.getCursor());
    }

    @Override
    public int getItemCount() {
        Log.w("count", Integer.toString(mCursor.getCount())); //This is executed
        return mCursor.getCount();
    }

    private class MySimpleCursorAdapter extends SimpleCursorAdapter {

        public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = mLayoutInflater.inflate(mLayoutToInflate, null);
            ....
            return view;
        }

        @Override
        public void bindView(final View view, Context context, Cursor cursor) {
            //Implemented this method for inflating all subviews inside each item of the list
        }
    }
 }

Note: I have gone through similar questions but the answers provided there are not relevant to my question because my RecyclerView is not inside ScrollView and also the getItemCount is returning the positive count.

Answer

xiaomi picture xiaomi · Sep 8, 2015

I think you forgot the setLayoutManager(LayoutManager) on the recycler view. Without Layout Manager, only getItemCount() is called.

Try with yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));