如何获取listview项目位置以及如何删除这些所有数据。
registerForContextMenu(listview);
///
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options");
menu.add(0, v.getId(), 0, "Delete");
menu.add(0, v.getId(), 0, "Cancel");
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo) menuInfo;
}
//我从contextItemSelelected调用
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Delete") {
//delete method by id from sqlite
} else if (item.getTitle() == "Cancel") {
return true;
} else {
return false;
}
return true;
}
答案 0 :(得分:0)
您可以在长按此列表项目的位置获取该项目
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
YourObj obj = (YourObj) listView.getItemAtPosition(position);
long id = obj.getId();
//Do what ever you want to do with id here!!!!
}