[英]How do I initialize a listener in a recycler adapter?
I need to be able to send the position of the tapped recycler item to the parent fragment by means of a listener but everytime I tap it, it says that the listener is null.我需要能够通过侦听器将点击的回收器项目的 position 发送到父片段,但每次点击它时,它都会说侦听器是 null。
java.lang.NullPointerException: Attempt to invoke interface method 'void com.eataway.partner.adapters.MenuAdapter$SectionAdapterListener.onSectionSend(int)' on a null object reference
at com.rest.partner.adapters.MenuAdapter.lambda$onBindViewHolder$0$MenuAdapter(MenuAdapter.java:82)
at com.rest.partner.adapters.-$$Lambda$MenuAdapter$TzEQ_dT_4Yb5iXO0LwjA0DjCNEI.onClick(Unknown Source:4)
Here is the relevant adapter code.这是相关的适配器代码。 At the top is the constructor class followed by the listener definition and setting.顶部是构造函数 class 后跟监听器定义和设置。 Then in the onBindViewHolder I trigger the click action for the item ie the entire view for that item is clickable.然后在 onBindViewHolder 中,我触发了该项目的点击操作,即该项目的整个视图都是可点击的。 And at the bottom is the inner class for the relevant viewHolder.底部是相关viewHolder的内部class。
// Constructor class
public MenuAdapter(Context adapterContext, ArrayList<DishItem> restaurantDishItemArrayList) {
this.adapterContext = adapterContext;
this.restaurantDishItemArrayList = restaurantDishItemArrayList;
}
// Define listener to send chosen section
public interface SectionAdapterListener {
void onSectionSend(int position);
}
// Set the listener. Must be called from the fragment
public void setSectionAdapterListener(SectionAdapterListener sectionAdapterListener) {
this.sectionAdapterListener = sectionAdapterListener;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (adapterContext instanceof SectionAdapterListener) sectionAdapterListener = (SectionAdapterListener) adapterContext;
if (holder instanceof SectionViewHolder) {
((SectionViewHolder)holder).tvHeaderName.setText(restaurantDishItemArrayList.get(position).getHeaderName());
if (sectionAdapterListener == null ) Log.d(TAG, "listener is null");
((SectionViewHolder)holder).viewForeground.setOnClickListener(v -> sectionAdapterListener.onSectionSend(position));
} else if (holder instanceof DishViewHolder) {
((DishViewHolder)holder).tvMenuItemName.setText(restaurantDishItemArrayList.get(position).getMenuItemName());
((DishViewHolder)holder).tvMenuItemDescription.setText(restaurantDishItemArrayList.get(position).getMenuItemDescription());
}
}
public class SectionViewHolder extends RecyclerView.ViewHolder {
private TextView tvHeaderName;
private RelativeLayout viewForeground;
public SectionViewHolder(@NonNull View itemView) {
super(itemView);
tvHeaderName = itemView.findViewById(R.id.menu_header_name);
viewForeground = itemView.findViewById(R.id.foreground_view);
}
}
Create an interface class.创建接口 class。 Then add the interface to your RecyclerAdapter constructor.然后将接口添加到您的 RecyclerAdapter 构造函数。
YourInterface listener;
public MenuAdapter(YourInterface listener, Context adapterContext, ArrayList<DishItem> restaurantDishItemArrayList) {
this.listener = listener;
this.adapterContext = adapterContext;
this.restaurantDishItemArrayList = restaurantDishItemArrayList;
}
Then on your activity然后在你的活动中
MenuAdapter menuAdapter = new MenuAdapter(listener, context, arraylist);
To avoid NullPointerException
check if the listener is null before using its method, so change onBindViewHolder()
to return if the sectionAdapterListener
is null
为避免NullPointerException
在使用其方法之前检查侦听器是否为 null,因此如果sectionAdapterListener
为null
,请更改onBindViewHolder()
以返回
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (adapterContext instanceof SectionAdapterListener) sectionAdapterListener = (SectionAdapterListener) adapterContext;
if (holder instanceof SectionViewHolder) {
((SectionViewHolder)holder).tvHeaderName.setText(restaurantDishItemArrayList.get(position).getHeaderName());
if (sectionAdapterListener == null ) {
Log.d(TAG, "listener is null");
return;
}
((SectionViewHolder)holder).viewForeground.setOnClickListener(v -> sectionAdapterListener.onSectionSend(position));
} else if (holder instanceof DishViewHolder) {
((DishViewHolder)holder).tvMenuItemName.setText(restaurantDishItemArrayList.get(position).getMenuItemName());
((DishViewHolder)holder).tvMenuItemDescription.setText(restaurantDishItemArrayList.get(position).getMenuItemDescription());
}
}
Otherwise you need to call setSectionAdapterListener()
from the activity/fragment that creates this adapter否则,您需要从创建此适配器的活动/片段中调用setSectionAdapterListener()
You can add below code into onBindViewHolder method of your adapter class..您可以将以下代码添加到适配器 class 的 onBindViewHolder 方法中。
holder.itemView.setonClickListener{
//add your code here....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.