从RecyclerView项目单击中删除SQLite中的行

时间:2017-02-02 09:54:26

标签: android sqlite android-recyclerview android-sqlite

我想从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();

}

}

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

1 个答案:

答案 0 :(得分:0)

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

public void deleteRow(String Mobile){

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

}