Android 事件总线不适用于两个事件监听器

标签 android greenrobot-eventbus

在我的 Android 应用程序中,我为 ListView 使用了一个 Activity 和 Adapter,我的要求是需要使用 EventBus 通过事件监听器与适配器类和 Activity 进行通信,因此我创建了两个事件监听器类。

我的流程是:

1) 我在 Activity 中有一个按钮,该按钮应该与 Adapter 类通信。

2) 如果我点击 TextView ( ListView 的 TextView 小部件)应该与 Activity 类通信。

通过以下代码,它适用于 Adapter 与 Activity 通信,但 Activity 不与适配器类通信。请帮助我如何在两个类(class)进行交流?

我已经发布了完整的示例项目代码:

Activity 类:

    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);
        }

    }

适配器类:

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);
    }
}

最佳答案

不要每次都创建新的 EventBus 实例,使用 EventBus.getDefault()。 添加到两个类方法 public void onEvent(Object event)

关于Android 事件总线不适用于两个事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30059531/

相关文章:

java - 最近一直在看很多第三方库代码,看到这段代码让我很困惑

android - 构建项目时出现 EventBus 异常 - 订阅者类没有名为 onEvent 的公共(public)方法

android - 绿色机器人 : EventBus's isRegistered() method not working as expected

java - 两个线程交互会减慢彼此的速度吗?

android - 无法解析配置 ':classpath' 的所有依赖项

android - 系统调用的钩子(Hook)方法是否可以在空参数引用中传递?

android - 同一类的多个粘性对象?

android - EventBus 发送空类

android - 带有共享首选项名称的 ListView 并将它们链接起来

android - Cordova Ionic 3 deviceready 未在 android 6 上启动