我想从“ SQLITE-DATABASE”中删除特定数据,但是它不起作用

时间:2019-12-21 11:20:18

标签: android android-recyclerview android-sqlite

我希望你一切都好.. 我在android的sqlite databasse中遇到问题,我想通过android中的cardview从sqlite dbbse中删除特定数据,但是当我取消对应用程序的安装并再次将其安装在移动设备中时,该数据不起作用,但是第一次删除数据后,却没有数据已从sqlite数据库中删除,数据已从cardview中删除,但未在sqlite数据库中删除。请出来。 提前致谢。这是我的代码。

我的适配器类

    package com.deitel.sqlitedatabase_recyclerview.adapterclass;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.deitel.sqlitedatabase_recyclerview.MainActivity;
import com.deitel.sqlitedatabase_recyclerview.R;
import com.deitel.sqlitedatabase_recyclerview.database.DatabaseHelper;
import com.deitel.sqlitedatabase_recyclerview.modelclass.NotepadModelClass;
import java.util.List;
public class NotepadAdapter extends RecyclerView.Adapter<NotepadAdapter.Viewholder> {
DatabaseHelper databaseHelper;
    public static List<NotepadModelClass> itemlist;
    public NotepadAdapter(@NonNull Context context, List<NotepadModelClass> itemlist) {
        this.itemlist = itemlist;
        databaseHelper=new DatabaseHelper((MainActivity) context);
    }
    @NonNull
    @Override
    public NotepadAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_cardview_layout, viewGroup, false);
        Viewholder holder = new Viewholder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull NotepadAdapter.Viewholder holder, final int position) {
        holder.textView_name.setText("Name :" + itemlist.get(position).getName());
        holder.textView_details.setText("Details : " + itemlist.get(position).getDetails());
        holder.textView_languages.setText("Language : " + itemlist.get(position).getLanguages());
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                databaseHelper.delete(position);
                itemlist.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,itemlist.size());
                notifyDataSetChanged();
            }
        });
    }
    @Override
    public int getItemCount() {
        return itemlist.size();
    }
    public class Viewholder extends RecyclerView.ViewHolder {
        TextView textView_name;
        TextView textView_details;
        TextView textView_languages;
        ImageButton btn_delete;
        public Viewholder(View view) {
            super(view);
            textView_name = view.findViewById(R.id.textview_name);
            textView_details = view.findViewById(R.id.textview_details);
            textView_languages = view.findViewById(R.id.textview_spinner);
            btn_delete=view.findViewById(R.id.btn_delete);
        }
    }
}

我的数据库助手类

    package com.deitel.sqlitedatabase_recyclerview.database;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.deitel.sqlitedatabase_recyclerview.MainActivity;
public class DatabaseHelper extends SQLiteOpenHelper {
    public static final  String DATABASE_NAME="Notepad.db";
    public static final String TABLE_NAME="notepad_table";
    public static final String COL_1="ID";
    public static final String COL_2="NAME";
    public static final String COL_3="DETAILS";
    public static final String COL_4="LANGUAGES";
    @NonNull
    @Override
    public String toString() {
        return super.toString();
    }
    public DatabaseHelper(@Nullable MainActivity context) {
        super(context, DATABASE_NAME, null, 2);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + "("
                + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COL_2 + " TEXT,"
                + COL_3 + " TEXT,"
                + COL_4 + " TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
        onCreate(db);
    }
    public boolean insertdata(String name,String details,String languages)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,details);
        contentValues.put(COL_4,languages);
        long result=db.insert(TABLE_NAME,null,contentValues);
        if (result== -1)
        {
            return false;
        }
        else {
            return true;
        }
    }
    public void delete(int id){
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_NAME,COL_1 + " = ?", new String[]{String.valueOf(id)});
        db.close();

    }

}



1 个答案:

答案 0 :(得分:0)

您正在使用列表中的位置,该位置并不总是单击行的 id

  • 如果单击第一个,则位置为0,因此不会删除行,因为最低的 id 为1(通常)。
  • 如果单击第二个,则位置 1 ,因此如果该行存在,则删除 id 1

制作项目列表时,您需要在 NotepadModelClass 中存储 id id 。您还需要在 NotepadModelClass 中具有 getId 方法。然后,您可以使用databaseHelper.delete(itemlist.get(position).getId());。然后将删除相应的行。


示例

NotepadModelClass.java

public class NotepadModelClass {

    int id; //<<<<<<<<< ADDED (should really be long but doesn't matter)
    String name;
    String details;
    String languages;

    NotepadModelClass(){}
    NotepadModelClass(String name, String details, String languages) {
    }
    NotepadModelClass(int id, String name, String details, String languages) {
        this.id = id;
        this.name = name;
        this.details = details;
        this.languages = languages;
    }

    /* <<<<<<<<<< ADDED >>>>>>>>>> */
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getLanguages() {
        return languages;
    }

    public void setLanguages(String languages) {
        this.languages = languages;
    }
}
  • 请注意,这可能大不相同,重要的部分已注释

DataabseHelper.java

唯一的变化是添加了以下方法:-

public List<NotepadModelClass> getAllNotepadModels() {
    ArrayList<NotepadModelClass> list = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor csr = db.query(TABLE_NAME,null,null,null,null,null,null);
    while (csr.moveToNext()) {
        list.add(new NotepadModelClass(
                csr.getInt(csr.getColumnIndex(COL_1)), //<<<<<<<<<< sets the ID
                csr.getString(csr.getColumnIndex(COL_2)),
                csr.getString(csr.getColumnIndex(COL_3)),
                csr.getString(csr.getColumnIndex(COL_4))
                )
        );
    }
    return list;
}
  • 重要方面是在NotepadModelClass中设置 id

NotepadAdapter.java

唯一的变化是:-

            //databaseHelper.delete(position);
            Log.d("BUTTONCLICKINFO","In the display item " + position + " was clicked. The respective rowid is " + itemlist.get(position).getId());
            databaseHelper.delete(itemlist.get(position).getId());

即正在从该位置的项目中检索ID,并添加了日志记录以显示差异。

结果

首次运行该应用程序时,将添加10行,并且显示为:-

enter image description here

依次按 Test2 Test5 Test7 的行的删除我!按钮。< / p>

日志显示:-

2019-12-22 09:20:38.508 D/BUTTONCLICKINFO: In the display item 1 was clicked. The respective rowid is 2
2019-12-22 09:20:40.685 D/BUTTONCLICKINFO: In the display item 3 was clicked. The respective rowid is 5
2019-12-22 09:20:42.361 D/BUTTONCLICKINFO: In the display item 4 was clicked. The respective rowid is 7
  • 即点击 TEST2 (第二行)(位置 1 )会导致 id 2 被删除。
  • 然后重新排序商品/位置
    • Test1 位于位置 0
    • Test3 位于位置 1
    • 所以ID为 5 Test5 ,原为位置 4 现在位于位置 3

显示为:-

enter image description here

应用已重新启动:-

enter image description here