2017-02-02 24 views
-1

RecyclerViewからSQLiteテーブル行を削除します。
私はベストを尽くして、StackOverflowで利用可能な回答をほとんどすべて使いました。
しかし、私は希望の結果を得ることができません。RecyclerViewアイテムからSQLiteの行を削除します。

マイアプリのクラッシュ私は私のRecyclerViewボタンをdeleterow()関数を呼び出すここ

をクリックして自分のコード

アダプタクラスcode`

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()); 


     } 
    }); 

databseクラスで私の削除目的球

です
public void deleteRow(String Mobile){ 

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

} 

ここに私の複雑ですeteデータベースクラス

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(); 

} 

} 

私の間違いはどこにありますか教えてください。

+0

あなたはlogcatを投稿してください。 –

+0

このリンクを参照してくださいhttp://stackoverflow.com/questions/41937027/sqlite-exception-while-trying-to-delete-row/41937288#41937288 – user7374191

+0

あなたも欠けているstartManagingCursor(カーソル); http://stackoverflow.com/questions/38628897/sqlite-getting-only-last-record-not-all-the-records/38873326で私の答えを見てください –

答えて

0

私はあなたが読み込み可能なデータベースの代わりに書き込み可能なデータベースを呼び出すべきだと思います。あなたの削除方法を示すようにしてください

public void deleteRow(String Mobile){ 

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

} 
関連する問題