-1

I checked full of Stackoverflow but i can't find solution, May be this is duplicate question but please help me. i create Interface in Adapter:-

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;
}

In onBindViewHolder:-

final AdvSearchList_Model item = listItem.get(position);
        myholder.setIsRecyclable(false);
        myholder.bind(position, item, listener);

Call bind function:-

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);
                }
            });}

and set onItemClick in Fragment 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;
            }
        }
    });

When i click on Item then its give me NullPointerException. Please do the needfull.

Amit Sharma
  • 261
  • 1
  • 4
  • 20
  • 3
    The needfull is that you post your stacktrace and google for `NullPointerException`> – blackapps Aug 21 '20 at 08:03
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Aug 22 '20 at 06:17

2 Answers2

2

add this method in your adapter -

 public void setOnItemListener(Context context, ArrayList<AdvSearchList_Model> dataList, OnItemClickListener listener) {    
    this.context = context;
    this.listItem = dataList;
    this.listener = listener;
 }

access your fragment -

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;
            }
        }
    });
0

you must down cast context in your adapter class to your listener field not pass listener to your adapter from outer class , do this in try catch like this :

    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 ");
      }

 }

you must implement this interface in your activity like a callback try this if don't work tell us more details.