在我的Android應用程序中,我使用了一個Activity和Adapter進行列表視圖,我的獲取需要通過EventBus通過事件監聽器來傳遞適配器類和活動,以便創建兩個事件監聽器類。Android事件總線不適用於兩個事件監聽器
我的過程是:
1)I具有在活動的按鈕時,按鈕應通信適配器類。 2)如果我點擊文本視圖(文本視圖小部件的列表視圖)應該溝通活動類。
通過以下代碼,它適用於適配器與Activity進行通信,但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'實例? – CommonsWare
請看我更新的問題。 –