I have an activity inherited from ListActivity and using AndroidAnnotations. While .onListItemClick
works fine, context menu for the list item does not appear at all and even .onCreateContextMenu
isn't called, but .onListItemClick
fires after a long tap to list item. Here's my code:
@OptionsMenu(R.menu.places)
@EActivity(R.layout.places)
public class PlacesPicker extends ListActivity {
private static String[] DATA_SOURCE = { PlacesDB.PLACE_NAME, PlacesDB.PLACE_DESC };
private static int[] DATA_DESTINATION = { R.id.place_name, R.id.place_desc };
public static ListView lv;
@Bean
PlacesDB db;
Cursor cursor;
@AfterInject
public void init() {
cursor = db.getPlaces(null, null);
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.place_item, cursor, DATA_SOURCE, DATA_DESTINATION);
setListAdapter(adapter);
lv = getListView();
registerForContextMenu(lv);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// It works fine
// It works even after long tap instead of context menu!
Toast.makeText(this, "Item clicked!", Toast.LENGTH_SHORT).show();
}
@OptionsItem
public void addPlace(){
// It works OK too
startActivity(new Intent(this, PlaceEditor_.class));
}
public void editPlace() {
// ...skipped for brevity...
}
public void deletePlace() {
// ...skipped...
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// This isn't ever called!
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.place_options, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// ...skipped...
}
}
Can anybody tell me what is wrong here? Thanks for advance.
I got it! It was my own mistake: the .init()
method must be annotated as @AfterViews
, not as @AfterInject
. It works like this: the ListActivity has a default ListView when created, then my bean PlacesDB is injected, after that fires @AfterInject
method, i.e. .init()
where the default ListView is registered for context menu, and after that my R.layout.places
is set as content view for activity with another ListView which is not registered! So, changing annotation to @AfterViews
makes .init()
to run after .setContentView(R.layout.places)
and then my own ListView from the layout is registered for context menu and everything begins to work :)