Sql lite数据库项未永久删除

时间:2018-05-13 00:52:12

标签: java android android-studio android-sqlite

我使用SQL lite数据库创建了一个应用程序,它在数据库中添加了一些杂货项目,并且有更新,添加,查看和删除项目的选项。所有方法都运行正常,但只有我关闭应用程序。我可以删除项目,但重新启动应用程序。他们没有被删除。

//数据库处理程序类中的DELETE方法

public void deleteGrocery(int id){


    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Constants.TABLE_NAME, Constants.KEY_ID + "=?", new String[]{String.valueOf(id)});
    db.close();

}

适配器

public  class rvAdapter {

     List<Grocery> groceries;

     public rvAdapter(Context context, List<Grocery> groceries) {


        this.context = context;
        this.groceries = groceries;
    }

// on clicking delete button

        public void onClick(View view) {

            switch (view.getId()){

                case R.id.deleteButton:

                    Grocery g2 = groceries.get(getAdapterPosition());
                    deleteItem(g2.getId());

                    break;
            }
        }
// Delete Item Method


         public void deleteItem(final int id){



            builder = new AlertDialog.Builder(context);
            inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.confirmation_dialog, null);

            Button dontDelete = view.findViewById(R.id.noButton);
            Button deleteItem = view.findViewById(R.id.YesButton);

            builder.setView(view);
            dialog = builder.create();
            dialog.show();

            dontDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });

            deleteItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // delete item from db
                    DatabaseHandler db = new DatabaseHandler(context);
                    db.deleteGrocery(id);
                    groceries.remove(getAdapterPosition());
                    notifyItemRemoved(getAdapterPosition());
                    dialog.dismiss();
                }
            });
        }
 }

0 个答案:

没有答案