RecyclerView onItemClick 不起作用,始终显示 nullpointerexception
RecyclerView onItemClick not working, always show nullpointerexception
我检查了整个 Whosebug,但找不到解决方案,可能这是重复的问题,但请帮助我。
我在适配器中创建接口:-
public interface OnItemClickListener {
void onItemClick(int positon, AdvSearchList_Model item, View view);
}
public MyMatches_New_Adapter(Context context, ArrayList<AdvSearchList_Model> dataList, OnItemClickListener listener) {
this.context = context;
this.listItem = dataList;
this.listener = listener;
}
在 onBindViewHolder 中:-
final AdvSearchList_Model item = listItem.get(position);
myholder.setIsRecyclable(false);
myholder.bind(position, item, listener);
调用绑定函数:-
public void bind(final int position, final AdvSearchList_Model item, final MyMatches_New_Adapter.OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(position, item, v);
}
});
user_pic_circle_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(position, item, v);
}
});}
并在 Fragment 中设置 onItemClick Class:-
adapter1 = new MyMatches_New_Adapter(context, listItem, new MyMatches_New_Adapter.OnItemClickListener() {
@Override
public void onItemClick(int positon, final AdvSearchList_Model item, View view) {
switch (view.getId()) {
case R.id.user_pic_circle_layout:
Toast.makeText(context, "COMMENT Working", Toast.LENGTH_SHORT).show();
break;
}
}
});
当我单击 Item 时,它会给我 NullPointerException。请做有需要的。
在你的适配器中添加这个方法 -
public void setOnItemListener(Context context, ArrayList<AdvSearchList_Model> dataList, OnItemClickListener listener) {
this.context = context;
this.listItem = dataList;
this.listener = listener;
}
访问您的片段 -
adapter1.setOnItemListener(context, listItem, new MyMatches_New_Adapter.OnItemClickListener() {
@Override
public void onItemClick(int positon, final AdvSearchList_Model item, View view) {
switch (view.getId()) {
case R.id.user_pic_circle_layout:
Toast.makeText(context, "COMMENT Working", Toast.LENGTH_SHORT).show();
break;
}
}
});
您必须将适配器 class 中的上下文向下转换为您的侦听器字段,而不是将侦听器从外部 class 传递给您的适配器,在 try catch 中这样做:
private Context mContext;
private OnItemClickListener mListener;
public MyMatches_New_Adapter(Context context, ArrayList<AdvSearchList_Model> dataList) {
this.mContext = context;
this.listItem = dataList;
try {
this.mListener = (OnItemClickListener ) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement OnItemClickListener ");
}
}
您必须在 activity 中像回调一样实现此接口
如果不起作用请尝试此操作,请告诉我们更多详细信息。
我检查了整个 Whosebug,但找不到解决方案,可能这是重复的问题,但请帮助我。 我在适配器中创建接口:-
public interface OnItemClickListener {
void onItemClick(int positon, AdvSearchList_Model item, View view);
}
public MyMatches_New_Adapter(Context context, ArrayList<AdvSearchList_Model> dataList, OnItemClickListener listener) {
this.context = context;
this.listItem = dataList;
this.listener = listener;
}
在 onBindViewHolder 中:-
final AdvSearchList_Model item = listItem.get(position);
myholder.setIsRecyclable(false);
myholder.bind(position, item, listener);
调用绑定函数:-
public void bind(final int position, final AdvSearchList_Model item, final MyMatches_New_Adapter.OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(position, item, v);
}
});
user_pic_circle_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(position, item, v);
}
});}
并在 Fragment 中设置 onItemClick Class:-
adapter1 = new MyMatches_New_Adapter(context, listItem, new MyMatches_New_Adapter.OnItemClickListener() {
@Override
public void onItemClick(int positon, final AdvSearchList_Model item, View view) {
switch (view.getId()) {
case R.id.user_pic_circle_layout:
Toast.makeText(context, "COMMENT Working", Toast.LENGTH_SHORT).show();
break;
}
}
});
当我单击 Item 时,它会给我 NullPointerException。请做有需要的。
在你的适配器中添加这个方法 -
public void setOnItemListener(Context context, ArrayList<AdvSearchList_Model> dataList, OnItemClickListener listener) {
this.context = context;
this.listItem = dataList;
this.listener = listener;
}
访问您的片段 -
adapter1.setOnItemListener(context, listItem, new MyMatches_New_Adapter.OnItemClickListener() {
@Override
public void onItemClick(int positon, final AdvSearchList_Model item, View view) {
switch (view.getId()) {
case R.id.user_pic_circle_layout:
Toast.makeText(context, "COMMENT Working", Toast.LENGTH_SHORT).show();
break;
}
}
});
您必须将适配器 class 中的上下文向下转换为您的侦听器字段,而不是将侦听器从外部 class 传递给您的适配器,在 try catch 中这样做:
private Context mContext;
private OnItemClickListener mListener;
public MyMatches_New_Adapter(Context context, ArrayList<AdvSearchList_Model> dataList) {
this.mContext = context;
this.listItem = dataList;
try {
this.mListener = (OnItemClickListener ) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement OnItemClickListener ");
}
}
您必须在 activity 中像回调一样实现此接口 如果不起作用请尝试此操作,请告诉我们更多详细信息。