android 从本地数据库中删除项功能不起作用

lnxxn5zx  于 2023-02-06  发布在  Android
关注(0)|答案(2)|浏览(135)
//the following function 'listens to the click' - here we are using an 'item click listener' which gives you a number of which one was clicked
        lv_customerList.setOnItemClickListener((parent, view, position, id) -> {
            //getting the customer that was just clicked to send it to the method to delete
            //the parent of the listener is the listview
            CustomerModel clickedCustomer = (CustomerModel) parent.getItemAtPosition(position); //this tells me which person was just clicked
            //now we call the "deleteOne" function from the databaseHelper class
            dataBaseHelper.deleteOne(clickedCustomer);
            //now we update the list view
            ShowCustomersOnListView(dataBaseHelper);
            Toast.makeText(MainActivity.this, "Deleted" + clickedCustomer,Toast.LENGTH_SHORT).show();
        });

这是主活动中的代码

//creating a new function that will delete contents from a database
    public boolean deleteOne(CustomerModel customerModel) {
        //find customerModel in the database. if is is found, delete it and return true, otherwise return false

        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "DELETE FROM" + CUSTOMER_TABLE + "WHERE"  + CUSTOMER_ID + " = " + customerModel.getId();

        Cursor cursor = db.rawQuery(queryString, null);

        if (cursor.moveToFirst()) {
            return true;
        }
        else {
            return false;
        }
    }

这是来自DatabaseHelper活动的代码
我遵循了https://youtu.be/312RhjfetP8教程,并完全复制了代码(尽管Android IDE建议更改...

new AdapterView.OnItemClickListener()

转换为lambda(在mainactivity的方法中)
但现在每当我点击应用程序中的一行(看视频了解我的意思)-应用程序只是崩溃,我不知道为什么:(
有人能帮帮我吗?
太感谢你了!

htzpubme

htzpubme1#

我敢肯定,您在查询String中遗漏了很多空格字符。

String queryString = "DELETE FROM" + CUSTOMER_TABLE + "WHERE"  + CUSTOMER_ID + " = " + customerModel.getId();

应改为

String queryString = "DELETE FROM " + CUSTOMER_TABLE + " WHERE "  + CUSTOMER_ID + " = " + customerModel.getId();
6l7fqoea

6l7fqoea2#

DeleteBuilder<CustomerModel, Integer> deletebuilder = 
                    mcustomermodel.deleteBuilder();

相关问题