8

In my Android application I have used an Activity and Adapter for list view, my acquirement is need to communicate both adapter class and activity via event listener using EventBus, so that I have created two event listener classes.

My process is:

1) I have a button in the activity,the button should communicate Adapter class.

2) If I click text view (text view widgets of list view) should communicate Activity class.

By the following code it works for Adapter communicates with Activity but Activity does not communicates with adapter class. Please help me on how to communicates for both the classes?

I have posted my full sample project code:

Activity class:

    public class ListMobileActivity extends Activity {....};

        private ListView list;
        private Button btn;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EventBus.getDefault().register(ListMobileActivity.this);
            ......
            list.setAdapter(adapter);

// Does not communicates with Adapter.
            btn.setOnClickListener(new OnClickListener() { 
                @Override
                public void onClick(View arg0) {
                    EventBus.getDefault().post(new TestEvent2("test event"));
                }
            });
        }

        public void onEvent(TestFinishedEvent event) {
            Log.e("TestFinishEvent ", event.test);
        }

    }

Adapter class:

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_mobile, values);
        this.context = context;
        this.values = values;
      EventBus.getDefault().register(this.context); // registered here.
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);
        .........
        // its works, communicate with Activity
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                EventBus.getDefault().post(new TestFinishedEvent("ssds"));
            }
        });
        return rowView;
    }

    public void onEvent(TestEvent2 event) {
        Log.e("Test event 2 ", event.test);
    }
}
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

6

Don't create new EventBus instance each time, use EventBus.getDefault(). Add to both classes method public void onEvent(Object event)

Natali
  • 2,934
  • 4
  • 39
  • 53
  • 1
    Thanks for your comment and please see my updated question for communication does not working for second class. – M.A.Murali May 06 '15 at 11:24
  • You forget to call EventBus.getDefault().register in second class – Natali May 06 '15 at 12:17
  • If I register event bus at constructor of adapter class I got the following exception. Caused by: de.greenrobot.event.EventBusException: Subscriber class com.mkyong.android.ListMobileActivity already registered to event class com.mkyong.android.TestFinishedEvent – M.A.Murali May 06 '15 at 13:01
  • 2
    Remove EventBus.getDefault().register(this.context) and add register EventBus.getDefault().register(adapter) in onCreate activity method. Also add to onDestroy activity method EventBus.getDefault().unregister(this); and EventBus.getDefault().unregister(adapter); – Natali May 06 '15 at 13:18
  • how to use event listener in BroadcastReceiver's onReceive method. if I use EventBus.getDefault().register(this); I got " has no method called onEvent" please help me. – M.A.Murali May 18 '15 at 13:40
  • Try to add method onEvent(Object o) in class where you want to call EventBus.getDefault().register(this) – Natali May 18 '15 at 14:41
4

In your MobileArrayAdapter constructor

change

 EventBus.getDefault().register(this.context)

to

EventBus.getDefault().register(this)

Edit 1:

Also be aware, that you always should call EventBus.getDefault().unregister(this); once you don't need to receive events or activity is beeing stopped/destroyed

Dominik Suszczewicz
  • 1,469
  • 2
  • 16
  • 23
  • how to use event listener in BroadcastReceiver's onReceive method. if I use EventBus.getDefault().register(this); I got " has no method called onEvent" please help me. – M.A.Murali May 18 '15 at 13:40
0

Your activity must register to EventBus.

In your Activity class Suscribe in on onStart() Unsuscribe in onStop()

In your adapter Just pour your event, all suscriber receive your post event.

Didier B.
  • 3
  • 3