简体   繁体   English

如何在回收器适配器中初始化侦听器?

[英]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,因此如果sectionAdapterListenernull ,请更改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.

相关问题 将侦听器传递给回收器适配器 - Passing a listener to a recycler adapter 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何在回收站适配器类中使用getcontext()? - How can I use getcontext() in recycler adapter class? 如何使用查询文本侦听器将 API 数据添加到片段的回收器视图以调用 GET 查询 - How do I add API data to a fragment's recycler view using query text listener to call the GET query 如何从Acivity类中在Recycler View Adapters上设置OnClick侦听器? 还是从第一个适配器访问其他RVAdapter? - How to set OnClick listener on Recycler View Adapters from within Acivity class? Or Access other RVAdapter from first Adapter? 如何在构造函数中传递 Recycler 视图适配器? - How to pass a Recycler view adapter in a constructor? 如何在回收器视图适配器中获取上下文 - How to get a context in a recycler view adapter 如何从回收商视图适配器中获取项目 - How to get items from recycler view adapter 如何设置监听器 - How do I set Listener on 如果我在回收器视图中有数千个项目,那么如何在每个项目上设置项目点击侦听器 - If I Have thousands of items inside the recycler view then How can I set the item click listener on each item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM