这不好。你必须调用方法registerForContextMenu
。onCreate
所以试试这样:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
registerForContextMenu(<yourListView>);
...
}
对于创建ContextMenu
你必须覆盖onCreateContextMenu
方法
@Override
public void onCreateContextMenu(ContextMenu cMenu, View parent, ContextMenu.ContextMenuInfo info) {
this.contextMenu = cMenu;
new MenuInflater(Contacts.this).inflate(R.menu.conmenu, this.contextMenu);
}
对于选择项目覆盖onContextItemSelected
方法:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.c_odobrat:
deleteContactDialog(info.id);
return true;
}
return false;
}
它应该有效。