I have a listView which does the following:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, TodoDetailActivity.class);
Uri todoUri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" + id);
i.putExtra(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri);
Toast.makeText(getApplicationContext(),""+id,Toast.LENGTH_SHORT).show();
startActivity(i);
}
Note, there is a long id which I use to pass to the content provider to open a new activity and edit the "Todo" which is associated with the id.
Now I am trying to delete a "todo" through a contextual menu.
However, I don't know how to get the item id:
this.getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
//Not Used
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_delete_multiple:
for (int i = adapter.getCount() - 1; i >= 0; i--) {
if (getListView().isItemChecked(i)) {
Toast.makeText(getApplicationContext(),""+i,Toast.LENGTH_SHORT).show();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuItem.getMenuInfo();
Uri uri = Uri.parse(MyTodoContentProvider.CONTENT_URI+"/"+ what'sTHEIDE???);
getContentResolver().delete(uri, null, null);
}
}
fillData();
actionMode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
actionMode = null;
}
});
In my onActionItemClicked()
area I would assume that I could get the same "long id" from the list view. What do I have to call to get the id to pass into the onActionItemClicked()
?
As a beginner android programmer, I am a bit flabbergasted.
Turns out I was overthinking this.
long mId = getListView().getAdapter().getItemId(i);
Uri uri = Uri.parse(MyTodoContentProvider.CONTENT_URI+"/"+ mId);
getContentResolver().delete(uri, null, null);
No complicated AdapterView methods are needed. info.id kept returning null since info was null so I just retrieved the id of the list view item that was clicked and passed it to the content provider to handle.