[英]RecyclerView.Adapter's onCreateViewHolder and onBindViewHolder methods are not getting called
I'm trying to implement TwoWayView in my project by following the sample project provided in that link. 我正在尝试通过遵循该链接中提供的示例项目在我的项目中实现TwoWayView 。 I have implemented everything and my code works without any errors but the actual List is not getting inflated.
我实现了一切,我的代码工作没有任何错误,但实际的List没有得到膨胀。 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. 在调试时我发现适配器设置正确,
getItemCount
方法也返回正数,但是没有调用onCreateViewHolder
和onBindViewHolder
上的其他两个重写方法。 I have no clue why it is not working. 我不知道为什么它不起作用。 Please see the partial code below, any help is appreciated.
请参阅下面的部分代码,任何帮助表示赞赏。 Thanks.
谢谢。
MyAdapter.java class MyAdapter.java类
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. 注意:我已经遇到了类似的问题,但是那里提供的答案与我的问题无关,因为我的
RecyclerView
不在ScrollView
,而且getItemCount
也返回正数。
I think you forgot the setLayoutManager(LayoutManager)
on the recycler view. 我想你忘了了
setLayoutManager(LayoutManager)
视图上的setLayoutManager(LayoutManager)
。 Without Layout Manager, only getItemCount()
is called. 如果没有布局管理器,则只调用
getItemCount()
。
Try with yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
尝试使用
yourRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
First, you should keep a reference to Context when the Adapter is instantiated: 首先,在实例化Adapter时,应该保留对Context的引用:
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: 然后,在onCreateViewHolder方法中,使用对Context的引用来为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: 然后在您的CursorAdapter中,按如下方式对视图进行膨胀:
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. 我有同样的问题,虽然我使用JSON调用来检索数据而不是光标适配器,这些更改解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.