@Override public void onCreateContextMenu( final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) { if (v.getId() == getListView().getId()) { final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; final Person person = this.persons.get(info.position); menu.setHeaderTitle(person.getId() + " | " + person.getName()); menu.add(Menu.NONE, MENU_ADD_APP, MENU_ADD_APP, "Add App"); menu.add(Menu.NONE, MENU_DELETE_PERSON, MENU_DELETE_PERSON, "Delete Person"); menu.add(Menu.NONE, MENU_EDIT_PERSON, MENU_EDIT_PERSON, "Edit Person"); } }
@Override public boolean onContextItemSelected(final MenuItem item) { final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); final int menuItemIndex = item.getItemId(); final Person person = this.persons.get(info.position); final LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); final EditText editText = (EditText) textEntryView.findViewById(R.id.edit_text_dialog); switch (menuItemIndex) { case MENU_ADD_APP: new AlertDialog.Builder(this) .setTitle("Add App") .setView(textEntryView) .setPositiveButton( "Add", new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int whichButton) { final App app = new App(); app.setPerson(person); app.setName(editText.getText().toString()); DemoORMLiteActivity.this.demoRepository.saveOrUpdateApp(app); DemoORMLiteActivity.this.listAdapter.notifyDataSetChanged(); } }) .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int whichButton) { /* User clicked cancel so do some stuff */ } }) .show(); break; case MENU_DELETE_PERSON: this.persons.remove(info.position); this.demoRepository.deletePerson(person); this.listAdapter.notifyDataSetChanged(); break; case MENU_EDIT_PERSON: // Set name for editing editText.setText(person.getName()); new AlertDialog.Builder(this) .setTitle("Edit Person") .setView(textEntryView) .setPositiveButton( "Update", new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int whichButton) { person.setName(editText.getText().toString()); DemoORMLiteActivity.this.demoRepository.saveOrUpdatePerson(person); DemoORMLiteActivity.this.listAdapter.notifyDataSetChanged(); } }) .show(); break; default: break; } return true; }