繁体   English   中英

从 RecyclerView Item 单击删除 SQLite 中的行

[英]Delete row in SQLite from a RecyclerView Item click

我想从 RecyclerView 中删除 SQLite 表行。
我尽力了,几乎使用了 StackOverflow 中所有可用的答案。
但我无法得到想要的结果。

当我在 RecyclerView 按钮单击上调用deleterow()函数时,我的应用程序崩溃

这是我的代码

适配器类代码`

public void onBindViewHolder(final ViewHolder holder, final int position) {

    pref = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = pref .edit();

    holder.addressline1.setText(dbList.get(position).getAddressline1());
    holder.adderssline2.setText(dbList.get(position).getAddressline2());
    holder.city.setText(dbList.get(position).getCity());

    int id = dbList.get(position).getID();
    String idfordelete =Integer.toString(id);
    holder.id.setText(idfordelete);

    holder.mRemoveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String idfordelete =dbList.get(position).getMobile().toString();
            Toast.makeText(context,idfordelete,Toast.LENGTH_SHORT).show();

            addressDataBaseAdapter.deleteRow(idfordelete);

            dbList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position,dbList.size());


        }
    });

我在数据库类中的删除功能

public void deleteRow(String Mobile){

       SQLiteDatabase db = this.getReadableDatabase();
       db.delete(STUDENT_TABLE, "MOBILE = ?", new String[]{Mobile});
       db.close();

}

这是我完整的数据库类

public class AddressDataBaseAdapter extends SQLiteOpenHelper {
private static final String DATABASE_NAME="address";
private static final int DATABASE_VERSION = 1;
private static final String STUDENT_TABLE = "address_table";
private static final String STU_TABLE = "create table "+STUDENT_TABLE + "( ID INTEGER  PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE TEXT ,ADDRESSLINE1 TEXT,ADDRESSLINE2 TEXT,CITY TEXT,STATE TEXT)";

Context context;

public AddressDataBaseAdapter(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(STU_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + STUDENT_TABLE);

    // Create tables again
    onCreate(db);
}
/* Insert into database*/
public void insertIntoDB(String name, String mobile, String addressline1, String addressline2, String city, String state){
    Log.d("insert", "before insert");

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put("Name", name);
    values.put("Mobile", mobile);
    values.put("Addressline1", addressline1);
    values.put("addressline2", addressline2);
    values.put("City", city);
    values.put("State", state);

    // 3. insert
    db.insert(STUDENT_TABLE, null, values);
    // 4. close
    db.close();
    Toast.makeText(context, "insert value", Toast.LENGTH_LONG);
    Log.i("insert into DB", "After insert");
}
/* Retrive  data from database */
public List<Address_Activity_DataModel> getDataFromDB(){
    List<Address_Activity_DataModel> modelList = new ArrayList<Address_Activity_DataModel>();
    String query = "select * from "+STUDENT_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);

    if (cursor.moveToFirst()){
        do {
            Address_Activity_DataModel model = new Address_Activity_DataModel();
            model.setName(cursor.getString(1));
            model.setMobile(cursor.getString(2));
            model.setAddressline1(cursor.getString(3));
            model.setAddressline2(cursor.getString(4));
            model.setCity(cursor.getString(5));
            model.setState(cursor.getString(6));

            modelList.add(model);
        }while (cursor.moveToNext());
    }


    Log.e("student data=====>>", modelList.toString());


    return modelList;
}


/*delete a row from database*/

public void deleteRow(String Mobile){

       SQLiteDatabase db = this.getReadableDatabase();
       db.delete(STUDENT_TABLE, "MOBILE = ?", new String[]{Mobile});
       db.close();

}

}

请告诉我我的错误在哪里。

我认为您应该调用可写数据库而不是可读数据库。 显示您的删除方法应该像

public void deleteRow(String Mobile){

       SQLiteDatabase db = this.getWritableDatabase();
       db.delete(STUDENT_TABLE, "MOBILE = ?", new String[]{Mobile});
       db.close();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM