1

I have a fragment ui which has a recyclerview-checkbox list. This fragment is part of a tablayout of two tabs hosted in a dialog fragment. The hosting fragment contains a button from which the ids/names of the checked recyclerview items are collected and used.

I have used green robot to post a list containing the selected items and retrieved them in the hosting fragment on button click but I'm getting NPE.

Here's my implementation:

POJO Class:

public class MySessionEvents {

public List<String> strings;

public MySessionEvents(List<String> strings) {
    this.strings = strings;
}

public List<String> getStrings() {
    return strings;
}
}

Adapter Logic:

viewholder.checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Log.d(TAG, "Session id:\t" + sessions.prog_sessionId);
                selectedSessionsList.add(sessions.prog_sessionId);

                Bus.getBus().post(new MySessionEvents(selectedSessionsList));

                for (int m = 0; m < selectedSessionsList.size(); m++) {
                    Log.d(TAG, "Session id in list:\t" + selectedSessionsList.get(m));
                }
            }
        }
    });

and to retrieve the strings, I have registered and unregistered the bus(skipped the code) and done this:

@Subscribe
public void retrieveMySessions() {
    Log.d(TAG, "From event:\t" + events.getStrings());
     //for (int i = 0; i < events.strings.size(); i++){
    for (int i = 0; i < events.getStrings().size(); i++){
        Log.d(TAG, "My Session Ids:\t" + events.strings.get(i));
    }
}

Was I wrong to post from my adapter as the tab fragment was having NPE when trying to collect the checked items and also is my retrieval correct? Thanks.

EDIT:--- ADD PARAMS TO SUBSCRIBE METHOD ---

//    public void retrieveMySessions(MySessionEvents events) {
@Subscribe
public void retrieveMySessions() {
    Log.d(TAG, "From event:\t" + events.getStrings());
//  for (int i = 0; i < events.strings.size(); i++){
    for (int i = 0; i < events.getStrings().size(); i++){
        Log.d(TAG, "My Session Ids:\t" + events.strings.get(i));
    }
}
Darth Plagueris
  • 339
  • 4
  • 20

3 Answers3

2

You need to pass parameter in below method

@Subscribe
public void retrieveMySessions(MySessionEvents event) {
    Log.d(TAG, "From event:\t" + events.getStrings());

    for (int i = 0; i < events.getStrings().size(); i++){
        Log.d(TAG, "My Session Ids:\t" + events.strings.get(i));
    }
}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
1

When you're using Event Bus, there are two actors that you need to keep remember; Publisher and Subscriber.

Publisher will tell the subscriber about the data changes. Subscriber is a listener for the data changes ie. listening for the data changes that publisher does. So, whenever your publisher tell the subscriber about the data changes with:

Bus.getBus().post(new MySessionEvents(selectedSessionsList));

The subscriber need to listen for it by using a method with the same object holding the data changes with:

@Subscribe
public void retrieveMySessions(MySessionEvents events) {
  // data changes is in the events parameter, do something about it.

  ...
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

I solved this problem because the method anotated with @subscribe doesn't need to be called anywhere. I simply performed the operation in the method and got the values I sent with the bus like below:

@Subscribe
public void retrieveMySessions(MySessionEvents events) {
    myItemsList = events.getStrings();
}

In this case, I have added the items to the list and used where I wanted like this:

NewProgramDialog dialog = new NewProgramDialog();

    Bundle bundle = new Bundle();
    String s = new Gson().toJson(myItemsList);
    bundle.putString(KEY_MY_SESSIONS, s);
    bundle.putBoolean("from_selections", true);

    dialog.setArguments(bundle);
    dialog.show(getChildFragmentManager(), "NewProgramDialog");

    dismiss();

I used Gson to retrieve the values from the list. Problem solved. Thanks to all who responded.

Darth Plagueris
  • 339
  • 4
  • 20