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

こ雲淡風輕ζ 提交于 2019-12-23 12:36:44

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/32463236/recyclerview-adapters-oncreateviewholder-and-onbindviewholder-methods-are-not-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!