許小美@Forgame.Taipei
Android的三鐵共構
Eventbus
Android Taipei - Dec 29 2014
public class Event {
public static class ItemListEvent {
private List<Item> items;
public ItemListEvent(List<Item> items) {
this.items = items;
}
public List<Item> getItems() {
return items;
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Register
EventBus.getDefault().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregister
EventBus.getDefault().unregister(this);
}
// Get listview items data from Network
EventBus.getDefault().post(new ItemListEvent(Item.ITEMS));
public void onEventMainThread(ItemListEvent event) {
setListAdapter(new ArrayAdapter<Item>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, event.getItems()));
}