How to use EventBus in Adapter?


How to use EventBus in Adapter?
In my application i want use EventBUs
for manage my event and i want use this into Adapter
and Fragment
.
In my adapter
i should call Event. when select one item i want call event.
For this i write below code in adapter
:
EventBUs
Adapter
Fragment
adapter
adapter
viewHolder.avatarItemList_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
//Update image
EventBus.getDefault().postSticky(new EventUpdateAvatar());
);
My Fragment codes:
@Override
public void onStart()
EventBus.getDefault().register(this);
super.onStart();
@Override
public void onDestroy()
//unregister event bus
EventBus.getDefault().unregister(this);
super.onDestroy();
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onUpdate(EventUpdateAvatar updateAvatar)
Log.e("EventLog", "Call");
But when click on item (into adapter
) not call event in fragment
!
adapter
fragment
How can i fix this bug?
clickListener
onClickListener
@salman, yes show log. but not call eventBus. can you help me? please
– Hock
53 mins ago
Remove
threadMode = ThreadMode.MAIN
and try again.– salman
47 mins ago
threadMode = ThreadMode.MAIN
@salman, not work me
– Hock
41 mins ago
It's strange, I wrote this code and it worked for me. Although I used just
@Subscribe
without any argument and post()
instead of postSticky()
– salman
37 mins ago
@Subscribe
post()
postSticky()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Add log at your
clickListener
to ensure thatonClickListener
works properly.– salman
56 mins ago