我有一個活動繼承自ListActivity並使用AndroidAnnotations。雖然.onListItemClick
工作正常,但列表項目的上下文菜單根本沒有出現,即使.onCreateContextMenu
未被調用,但.onListItemClick
在長按一下列表項目後也會觸發。這裏是我的代碼:ListActivity中的AndroidAnnotations和上下文菜單
@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...
}
}
有人可以告訴我這裏有什麼問題嗎?感謝您的提前。