1

This is question is based on this answer.

I'm trying to pass a string from my RecyclerView.Adapter to my fragment.

In my adapter I added the following:

onItemClickListner onItemClickListner;

public void setOnItemClickListner(VideoAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner{
    void onClick(String str);//pass your object types.
}

I pass the following string once the amount of items in my adapter is less then one (adapter is empty):

onItemClickListner.onClick("TESTING");

Then, in my fragment I add the following:

//I do the following after setting my adapter
videoAdapter.setOnItemClickListner(new VideoAdapter.onItemClickListner() {
        @Override
        public void onClick(String str) {
            Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
        }
    });

For some reason the string is empty/null and the crash points to onItemClickListner.onClick("TESTING");.

Can someone please have a look and see what I might be doing wrong?


Edit:

I call onItemClickListner.onClick("TESTING"); inside my OnMenuItemClickListener as shown below:

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

                case R.id.delete:

                    onItemClickListner.onClick("TESTING");

                return true;

}

I did not provide my entire fragment because I just add the above inside onCreateView.

ankuranurag2
  • 2,300
  • 15
  • 30
HB.
  • 4,116
  • 4
  • 29
  • 53
  • On what layout view did you set `onClickListener`? Just show method, where you are calling `onItemClickListner.onClick("TESTING");` – grabarz121 Jan 24 '19 at 10:06
  • Yes, please post the whole fragment code here. Also whenever there are crashes it helps to have the crash log. Please always provide as much context as possible so we can help you out. – Joaquim Ley Jan 24 '19 at 10:09
  • @grabarz121 please see my edit – HB. Jan 24 '19 at 10:15
  • @JoaquimLey please see my edit, I didn't add the entire fragment because all I do is add the above in `onCreateView` after I initialise my adapter. – HB. Jan 24 '19 at 10:16
  • Is your `popup` initialized? Remember that, each switch statement should has a default case, which in your code should return false – grabarz121 Jan 24 '19 at 10:27
  • I still need to see the crash log and maybe more context to when how are you initing all your vars. – Joaquim Ley Jan 24 '19 at 10:29
  • @grabarz121 yes that works perfectly fine. I just copied a part to demonstrate. The issues is not related to the switch statement. – HB. Jan 24 '19 at 10:30
  • @JoaquimLey I think there is enough context on how I implemented it. As for the crash log, I mentioned the following in the question - `For some reason the string is empty/null and the crash points to onItemClickListner.onClick("TESTING");.` in other words the `String` is empty. – HB. Jan 24 '19 at 10:32
  • Alright, @HB. good luck, I hope someone else can help you because that's I'm trying to do but you don't want to provide the required details. – Joaquim Ley Jan 24 '19 at 10:41
  • @JoaquimLey I have added all the required details. Thanks for your time. – HB. Jan 24 '19 at 10:43
  • Lets start from the beginning... what do you wish to achieve? Does 'PopupMenu' is a part of your adapter item? – grabarz121 Jan 24 '19 at 11:17
  • @grabarz121 I would like to let the fragment know when the adapter is empty so that I can change text in my fragment. Yes the PopupMenu is part of my adapter – HB. Jan 24 '19 at 11:33
  • Adapter contains videos, right? Do you really need to have a custom menu for each item in adapter? You can simply use`onCreateContextMenu`, and register it on your RecyclerView. – grabarz121 Jan 24 '19 at 11:52
  • @grabarz121 The `PopupMenu` works perfectly fine as I want it to work, how will that solve the issue I have? – HB. Jan 24 '19 at 12:02
  • Can you show all code, what applies `popup`? I've added to my test project PopupMenu, and it's working perfect. Your problem must be in a place, where your code is used. – grabarz121 Jan 24 '19 at 21:45

3 Answers3

0

Have you noticed this? from your reference url

onItemClickListner your_interface;

public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.your_interface= onItemClickListner;
}

this.onItemClickListner = onItemClickListner this would be the self assignment. which may be causing the issue .

here your_interface is being initialised . I suspect that your onItemClickListner is not initialised .

So try by changing the name

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
0

Here's part of my working RecyclerView.Adapter example

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        final PopupMenu popupMenu = new PopupMenu(context, holder.itemView);
        popupMenu.inflate(R.menu.simple_menu);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        if (onEmptyListListener != null) {
                            onEmptyListListener.onEmpty("example string");
                        }
                        return true;
                    default:
                        return false;
                }
            }
        });
        final String name = values.get(position);
        holder.txtHeader.setText(name);
        holder.txtFooter.setText("Footer: " + name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupMenu.show();
            }
        });
    }

Rest of code is similar that yours, toasts are displaying properly.

grabarz121
  • 616
  • 5
  • 13
  • Sorry but I can't see how this has anything to do with the question? Like I said - `The PopupMenu works perfectly fine as I want it to work`. I only copied part of my `PopupMenu` just to demonstrate where I'm using `onItemClickListner.onClick("TESTING");` – HB. Jan 25 '19 at 16:09
  • I have actually solved this issue, I just didn't have time to provide my answer. – HB. Jan 25 '19 at 16:10
0

Firstly, thank you for all the answers and comments.

I fixed this issue by doing the following.


In my Adapter I added the following:

private UpdateInterface listner;

public interface UpdateInterface {
    void recyclerviewOnUpdate(int amount);
}

public VideoAdapter(Context context, ArrayList<PathModel> thumbPathList, ArrayList<PathModel> videoPathList, UpdateInterface listner) {
    this.context = context;
    this.thumbPathList = thumbPathList;
    this.videoPathList = videoPathList;
    //I added this
    this.listner=listner;
}

I call the interface the same way as I did in my question:

//Passing the amount of items in adapter
listner.recyclerviewOnUpdate(getItemCount());

It's very similar to what I've done above but now I implement the interface in my fragment like this:

public class VideoFragment extends Fragment implements VideoAdapter.UpdateInterface

and now I have a method to work with, shown below:

@Override
public void recyclerviewOnUpdate(int amount) {
    //Now I can get the int and check if the adapter is empty after removing a item
    if (amount<1) {
        txtEmptyAdapter.setVisibility(View.VISIBLE);
    }
}
HB.
  • 4,116
  • 4
  • 29
  • 53
  • so you changed from `videoAdapter.setOnItemClickListner` to implementing it in fragment. Glad you solved your issue. – Tejas Pandya Jan 26 '19 at 08:02