Android event bus does not work with two event listeners

In my Android app, I used Activity and Adapter to view the list, I need to exchange both the adapter class and the activity through the event listener using EventBus, so I created two classes of event listener.

My process:

1) I have a button in activity, the button should inform the adapter class.

2) If you click on the text view (widgets are the type of text in the list view), you must pass the Activity class.

In the following code, it works so that the Adapter communicates with the Activity, but the Activity does not interact with the adapter class. Please help me on how to communicate for both classes?

I posted the full project code:

Action 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); } } 
+7
android greenrobot-eventbus
source share
3 answers

Do not create a new instance of EventBus every time, use EventBus.getDefault() . Add the public void onEvent(Object event) method to both classes

+6
source share

In your constructor MobileArrayAdapter

change

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

to

 EventBus.getDefault().register(this) 

Change 1:

Also keep in mind that you should always call EventBus.getDefault().unregister(this); as soon as you do not need to receive events or activity is terminated / destroyed

+4
source share

Your activity must register with EventBus.

In your Activity class Subscribe to onStart () Unsuscribe in onStop ()

In your adapter Just pour your event, all subscribers will receive your message.

0
source share

All Articles