EventBus3.0 List事件遇到的坑

当你需要发送接收List类型的对象时,务必做个类型判断。。。否则会出现 java.lang.ClassCastException
具体操作如下

/*instanceof  to avoid some bugs...*/
    @Subscribe(sticky = true)
    public void getMemList(List mettings) {
        for (Object obj : mettings) {
            if (!(obj instanceof MettingMem.Mem)) {
                return;
            }
        }
        this.mettings = mettings;
        Logger.d("mettings size"+mettings.size());
    }

github上关于我的疑问有人回答了,最好不要传递List类型的。封装成bean再进行事件收发,具体实现如下:

class MeetingsEvent {

      public final List mettings;

      MeetingsEvent(List mettings) {
              this.mettings = mettings
      }
}

@Subscribe(sticky = true)
public void getMettingMem(MeetingsEvent event) {
    this.mettings = event.mettings;
    Logger.d("msgSize"+this.mettings.size());
}

issues地址,,,见笑了
https://github.com/greenrobot/EventBus/issues/497

你可能感兴趣的:(Java学习,Android开发,安卓基础)