gpt4 book ai didi

android - 具有多种 View 类型的 CursorRecyclerViewAdapter

转载 作者:太空狗 更新时间:2023-10-29 14:05:31 53 4
gpt4 key购买 nike

我正在使用来自 this code snippet 的 CursorRecyclerViewAdapter

当我在 recyclerview 中使用 Single Viewtype 时它工作正常。但是如果我使用多个 viewtypes 我会崩溃说 int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType null object reference/p>

我使用下面的代码来扩展 CursorRecyclerViewAdapter

public class PostCursorAdapter extends CursorRecyclerViewAdapter<RecyclerView.ViewHolder> {

private static final int VIEW_HOLDER_TYPE_1=1;
private static final int VIEW_HOLDER_TYPE_2=2;
private Integer viewtype=0;

public PostCursorAdapter(Context context,Cursor cursor){
super(context,cursor);

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
PostItem postitem = PostItem.fromCursor(cursor);
Log.d("PostItem",postitem.toString());
int viewtype=1;

if (postitem.isFromMe()==true){
viewtype=1;
}
else {
viewtype=2;
}

switch (viewtype) {

case VIEW_HOLDER_TYPE_1:
ViewHolder_Type1 viewholder1 = (ViewHolder_Type1) holder;
TextView mytimeView = (TextView) viewholder1.mytimeTextView;
mytimeView.setText(postitem.getPostTime());
TextView mymsgView = (TextView) viewholder1.mymessageTextView;
mymsgView.setText(postitem.getPostMsg());
break;

case VIEW_HOLDER_TYPE_2:
ViewHolder_Type2 viewholder2 = (ViewHolder_Type2) holder;
TextView timeView = (TextView) viewholder2.timeTextView;
timeView.setText(postitem.getPostTime());
TextView msgView = (TextView) viewholder2.messageTextView;
msgView.setText(postitem.getPostMsg());
break;

default:
break;
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;

switch (viewType) {

case VIEW_HOLDER_TYPE_1:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.mypost, parent, false);
ViewHolder_Type1 vh1 = new ViewHolder_Type1(v);
return vh1;

case VIEW_HOLDER_TYPE_2:
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post, parent, false);
ViewHolder_Type2 vh2 = new ViewHolder_Type2(v);
return vh2;

default:
break;
}

return null;
}
public static class ViewHolder_Type1 extends RecyclerView.ViewHolder {
public TextView mymessageTextView, mytimeTextView;
public ViewHolder_Type1(View v) {
super(v);
this.mymessageTextView = (TextView) v.findViewById(R.id.mymessageTextView);
this.mytimeTextView = (TextView) v.findViewById(R.id.mytimeTextView);
}


}

public static class ViewHolder_Type2 extends RecyclerView.ViewHolder {
public TextView messageTextView, timeTextView;
public ViewHolder_Type2(View v) {
super(v);
this.messageTextView = (TextView) v.findViewById(R.id.messageTextView);
this.timeTextView = (TextView) v.findViewById(R.id.timeTextView);
}
}}

最佳答案

我是这样解决的

创建一个私有(private) Cursor 并赋值给它

    public PostCursorAdapter(Context context,Cursor cursor){
super(context,cursor);
mCursor=cursor;

}

实现getItemViewType

 @Override
public int getItemViewType(int position) {
if (mCursor.moveToPosition(position))
{
PostItem postItem=PostItem.fromCursor(mCursor);
if (postItem.isFromMe())
{
return VIEW_HOLDER_TYPE_1;

}
else
{
return VIEW_HOLDER_TYPE_2;

}
}
return 1;

}

更改onBindViewHolder

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
PostItem postitem = PostItem.fromCursor(cursor);

switch (getItemViewType(cursor.getPosition())) {

case VIEW_HOLDER_TYPE_1:
ViewHolder_Type1 viewholder1 = (ViewHolder_Type1) holder;
TextView mytimeView = (TextView) viewholder1.mytimeTextView;
mytimeView.setText(postitem.getPostTime());
TextView mymsgView = (TextView) viewholder1.mymessageTextView;
mymsgView.setText(postitem.getPostMsg());
break;

case VIEW_HOLDER_TYPE_2:
ViewHolder_Type2 viewholder2 = (ViewHolder_Type2) holder;
TextView timeView = (TextView) viewholder2.timeTextView;
timeView.setText(postitem.getPostTime());
TextView msgView = (TextView) viewholder2.messageTextView;
msgView.setText(postitem.getPostMsg());
break;

default:
break;
}
}

关于android - 具有多种 View 类型的 CursorRecyclerViewAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664930/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com