从Listview item中删除数据。我可以从sqlite中删除listview项目位置的所有数据

时间:2015-05-28 06:36:02

标签: java android android-listview

如何获取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;
}

1 个答案:

答案 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!!!!
}