简体   繁体   中英

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.

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. Then in the onBindViewHolder I trigger the click action for the item ie the entire view for that item is clickable. And at the bottom is the inner class for the relevant viewHolder.

// 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. Then add the interface to your RecyclerAdapter constructor.

 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

@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

You can add below code into onBindViewHolder method of your adapter class..

holder.itemView.setonClickListener{
 //add your code here....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM