如何使用SQLiteOpenHelper从Android上的List中删除项目?

时间:2016-03-16 19:08:29

标签: java android listview

目前,在另一个class中,我需要在上传成功文件调用deleteCallWhenUploadSuccess时删除1个项目。 在本课程中,我使用fileName来确定需要删除的项目。

但它不会从布局activity_call_history.xml

中删除ListView中的项目

在DAO类中,我删除了代码:

public void deleteCallWhenUploadSuccess(String fileNameWhis)
{
    db = callDatabaseHelper.getWritableDatabase();
    String where = CallDatabaseHelper.FILE_NAME + "='" + fileNameWhis + "'";
    db.delete(CallDatabaseHelper.TABLE_NAME, where, null);
}

在其他课程中我使用它:

DAO.deleteCallWhenUploadSuccess(filename);

我编写代码以在事件onActionItemClicked上删除ListView上的项目。

DAO class我开始删除带有rowId的项目:

public void deleteCallV2(int rowId) {
    db = callDatabaseHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from "+CallDatabaseHelper.TABLE_NAME+" where rowId = "+String.valueOf(rowId),null);
    while(cursor.moveToNext()){
    this.rowId = CallDatabaseHelper.ROW_ID +"="+cursor.getString(cursor.getColumnIndex(CallDatabaseHelper.ROW_ID));
    }
    db.delete(CallDatabaseHelper.TABLE_NAME, this.rowId, null);

}

代码我曾经在HistoryFragment.java中删除了LayoutView of Layout historyAdapter中的代码,我在代码中的注释很容易阅读:

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.deleteAction:
            // Calls getSelectedIds method from ListViewAdapter Class
            selected = historyAdapter
                    .getSelectedIds();
            // Captures all selected ids with a loop
            for (int i = (selected.size() - 1); i >= 0; i--) {
                if (selected.valueAt(i)) {
                    Call selecteditem = historyAdapter
                            .getItem(selected.keyAt(i));
                    // Remove selected items following the ids

                    historyAdapter.remove(selecteditem);
                }
            }
                    getFragmentManager().beginTransaction().replace(R.id.container,new HistoryFragment()).commit();
            // Close CAB
            mode.finish();
            return true;
        case R.id.allAction:
            historyAdapter.toggleAll(listView);
            Toast.makeText(getActivity(), "Đã chọn tất cả", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return false;
    }
}

但只有当我选择长按项目并选择项目或选择全部删除时,它才有效。它使用ID删除项目。

1 个答案:

答案 0 :(得分:0)

由于我没有得到多少但根据问题的标题说要删除列表中的项目,我建议您在删除视图时使用notifyDataSetChanged(),以便列表视图可以自行更新每当它改变

演示此任务的一些代码是

adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
    adb.setTitle("Delete?");
    adb.setMessage("Are you sure you want to delete " + position);
    final int positionToRemove = position;
    adb.setNegativeButton("Cancel", null);
    adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            MyDataObject.remove(positionToRemove);
            adapter.notifyDataSetChanged();
        }});
    adb.show();
    }
});