无法从列表视图(待办事项列表)中删除项目,长按没有任何反应

Can't remove item from Listview(To do list), nothing is happening on long press

我正在尝试使用 sqlite 制作一个待办事项列表,当我长时间按下某个项目时,它将从列表(以及数据库)中删除。但我无法实现,无法理解如何删除(我是新手)。我还可以在 setOnItemLongClickListener 中做什么来从数据库中删除值? 这是我的 MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;
    private EditText e;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DatabaseHelper helper = new DatabaseHelper(this);
        lvItems = (ListView)findViewById(R.id.lvItems);
        items = helper.getAllAcontacts();
        itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        lvItems.setAdapter(itemsAdapter);
        e = (EditText)findViewById(R.id.etNewItem);
        Button add = (Button)findViewById(R.id.btnAddItem);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!e.getText().toString().isEmpty()) {
                    if (helper.insert(e.getText().toString())){
                        Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Not Inserted", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    e.setError("Enter To Do List Item");

                }
                items.clear();
                items.addAll(helper.getAllAcontacts());
                itemsAdapter.notifyDataSetChanged();
                lvItems.invalidateViews();
                lvItems.refreshDrawableState();
                e.setText("");
            }
        });


setupListViewListener();

    }
private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View item, int pos, long id) {
                        // Remove the item within array at position
                        items.remove(pos);
                        

                        itemsAdapter.notifyDataSetChanged();
                        // Return true consumes the long click event (marks it handled)
                        return true;

                    }
                });

    }

这是我的数据库删除函数:

public void deleteItem(long itemId) {

        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(DatabaseHelper.LIST_TABLE_NAME, "_id = ?",
                new String[] { String.valueOf(itemId) });
    }

我能够使用游标解决它。主要问题是,我的删除函数无法获取 ID,因此使用光标指向我想要删除的特殊位置。工作代码是:

private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View item, int pos, long id) {

                        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
                        dialogBuilder.setTitle("DELETE OR UPDATE");
                        dialogBuilder.setMessage("Write in text edit box and the tap on UPDATE to update value or simply tap on DELETE to delete value.");
                        dialogBuilder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                if(!e.getText().toString().isEmpty()) {
                                    if (helper.update(e.getText().toString(), pos)){
                                        Toast.makeText(MainActivity.this, "Updated!", Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(MainActivity.this, "Not Updated", Toast.LENGTH_SHORT).show();
                                    }
                                }
                                else{
                                    e.setError("Enter To Do List Item");
                                }
                                items.clear();
                                items.addAll(helper.getAllAcontacts());
                                itemsAdapter.notifyDataSetChanged();
                                lvItems.invalidateViews();
                                lvItems.refreshDrawableState();
                                e.setText("");

                            }
                        });

                        dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                items.remove(pos);

                                helper.deleteItem(pos);

                                itemsAdapter.notifyDataSetChanged();
                                dialog.cancel();
                            }
                        });
                        dialogBuilder.create();
                        dialogBuilder.show();

                        return true;

                    }
                });

    }

并且在 DatabaseHelper 中:

public void deleteItem(int pos) {
        SQLiteDatabase db = this.getWritableDatabase();
        List<Integer> database_ids= new ArrayList<Integer>();

        Cursor c = db.rawQuery("SELECT*FROM "+LIST_TABLE_NAME,null);
        while(c.moveToNext()){
            database_ids.add(Integer.parseInt(c.getString(0)));
        }

        db.delete(LIST_TABLE_NAME, " id=?",
                new String[] { String.valueOf(database_ids.get(pos)) });
        c.close();
    }