4

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.

Prudhvi
  • 2,276
  • 7
  • 34
  • 54

2 Answers2

9

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));

xiaomi
  • 6,553
  • 3
  • 29
  • 34
  • Hey, thanks for the reply but I'm not using the default _LinearLayoutManager_. If you see the sample code [here](https://github.com/lucasr/twoway-view/blob/master/sample/src/main/java/org/lucasr/twowayview/sample/LayoutFragment.java) (line 128) he's also not using _setLayoutManager_. – Prudhvi Sep 08 '15 at 18:24
  • So did you put into the xml file `app:twowayview_layoutManager="ListLayoutManager"` ? (or whatever layoutmanager) like this https://github.com/lucasr/twoway-view/blob/master/sample/src/main/res/layout/layout_list.xml#L25 – xiaomi Sep 08 '15 at 23:17
  • Yes, I put that in my XML file. – Prudhvi Sep 09 '15 at 14:12
0

First, you should keep a reference to Context when the Adapter is instantiated:

public class MyAdapter extends RecyclerView.Adapter < MyViewHolder > {

private MySimpleCursorAdapter mCursor;
private Context context;  // <-- add this line
//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); 
    this.context = context;  // <-- add this line
    //Passed necessary arguments
    ....
}

Then, in the onCreateViewHolder method, use that reference to Context to create the view for the ViewHolder:

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

Then in your CursorAdapter, inflate the view as follows:

        public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(mLayoutToInflate, parent, false);
        return view;
    }

I had the same issue, albeit I using a JSON call to retrieve the data instead of the Cursor Adapter, and these changes fixed the problem.

dpspae03
  • 79
  • 1
  • 10