0

我想从我的RecyclerAdapter中的mydatabase中删除数据。从RecyclerAdapter中的SQLite数据库中删除数据

我也有一个滑动类,从RecyclerView中删除项目。
现在,我可以从RecyclerView中删除项目,但不能从数据库中删除项目,因为我的数据库无法在RecyclerAdapter上调用。

这是我的ManagerActivity。

public class Manager2 extends AppCompatActivity { 
    MySQLiteHelper db = new MySQLiteHelper(this); 
    RecyclerView recycler; 
    RecyclerAdapter adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_manager2); 
     initializeViews(); 

     recycler.setHasFixedSize(true); 
     recycler.setLayoutManager(new LinearLayoutManager(this)); 
     recycler.setAdapter(adapter); 

     ItemTouchHelper.Callback callback = new Swipe(adapter); 
     ItemTouchHelper helper = new ItemTouchHelper(callback); 
     helper.attachToRecyclerView(recycler); 
    } 

    public void initializeViews(){ 
     List<Password> myPasswords = db.getAllPasswords(); 
     adapter = new RecyclerAdapter(myPasswords); 
     recycler = (RecyclerView)findViewById(R.id.recycler); 
    } 
} 

SQLite数据库类

public class MySQLiteHelper extends SQLiteOpenHelper { 

    // Passwords table name 
    public static final String TABLE_BOOKS = "passwords"; 

    // Passwords Table Columns names 
    public static final String KEY_ID = "id"; 
    public static final String KEY_TITLE = "title"; 
    public static final String KEY_PASSWORD = "password"; 

    public static final String[] COLUMNS = {KEY_ID,KEY_TITLE,KEY_PASSWORD}; 

    // Database Version 
    public static final int DATABASE_VERSION = 1; 
    // Database Name 
    private static final String DATABASE_NAME = "PasswordDB"; 

    public MySQLiteHelper(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // SQL statement to create password table 
     String CREATE_PASSWORD_TABLE = "CREATE TABLE passwords (" + 
       "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "title TEXT, "+ 
       "password TEXT)"; 

     // create books table 
     db.execSQL(CREATE_PASSWORD_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older passwords table if existed 
     db.execSQL("DROP TABLE IF EXISTS passwords"); 
     // create fresh passwords table 
     this.onCreate(db); 
    } 

    public void addPassword(Password password){ 
     //for logging 
     Log.d("addBook", password.toString()); 

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

     // 2. create ContentValues to add key "column"/value 
     ContentValues values = new ContentValues(); 
     values.put(KEY_TITLE, password.getTitle()); // get title 
     values.put(KEY_PASSWORD, password.getPassword()); // get password 

     // 3. insert 
     db.insert(TABLE_BOOKS, // table 
       null, //nullColumnHack 
       values); // key/value -> keys = column names/ values = column values 

     // 4. close 
     db.close(); 
    } 

    public Password getPassword(int id){ 

     // 1. get reference to readable DB 
     SQLiteDatabase db = this.getReadableDatabase(); 

     // 2. build query 
     Cursor cursor = 
       db.query(TABLE_BOOKS, // a. table 
         COLUMNS, // b. column names 
         " id = ?", // c. selections 
         new String[] { String.valueOf(id) }, // d. selections args 
         null, // e. group by 
         null, // f. having 
         null, // g. order by 
         null); // h. limit 

     // 3. if we got results get the first one 
     if (cursor != null) 
      cursor.moveToFirst(); 

     // 4. build password object 
     Password password = new Password(); 
     password.setId(Integer.parseInt(cursor.getString(0))); 
     password.setTitle(cursor.getString(1)); 
     password.setPassword(cursor.getString(2)); 

     //log 
     Log.d("getPassword("+id+")", password.toString()); 

     // 5. return book 
     return password; 
    } 


    public List<Password> getAllPasswords() { 
     List<Password> passwords = new LinkedList<Password>(); 

     // 1. build the query 
     String query = "SELECT * FROM " + TABLE_BOOKS; 

     // 2. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 

     // 3. go over each row, build book and add it to list 
     Password password = null; 
     if (cursor.moveToFirst()) { 
      do { 
       password = new Password(); 
       password.setId(Integer.parseInt(cursor.getString(0))); 
       password.setTitle(cursor.getString(1)); 
       password.setPassword(cursor.getString(2)); 

       // Add password to passwords 
       passwords.add(password); 
      } while (cursor.moveToNext()); 
     } 

     Log.d("getAllPasswords()", passwords.toString()); 

     // return passwords 
     return passwords; 
    } 

    public int updatePassword(Password password) { 

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

     // 2. create ContentValues to add key "column"/value 
     ContentValues values = new ContentValues(); 
     values.put(KEY_TITLE, password.getTitle()); // get title 
     values.put(KEY_PASSWORD, password.getPassword()); // get password 

     // 3. updating row 
     int i = db.update(TABLE_BOOKS, //table 
       values, // column/value 
       KEY_ID+" = ?", // selections 
       new String[] { String.valueOf(password.getId()) }); //selection args 

     // 4. close 
     db.close(); 

     return i; 
    } 

    public boolean updatePass(int id, String title, String password){ 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_ID,id); 
     values.put(KEY_TITLE,title);// get title 
     values.put(KEY_PASSWORD, password); // get password 
     db.update(TABLE_BOOKS,values,"id = ?",new String[] {String.valueOf(id)}); 
     return true; 
    } 

    public void deletePassword(Password password) { 

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

     // 2. delete 
     db.delete(TABLE_BOOKS, //table name 
       KEY_ID+" = ?", // selections 
       new String[] { String.valueOf(password.getId()) }); //selections args 

     // 3. close 
     db.close(); 

     //log 
     Log.d("deletePassword", password.toString()); 
    } 
} 

这是我RecyclerAdapter类

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 
    List<Password> myPasswords; 
    public RecyclerAdapter(List<Password> myPasswords) { 
     this.myPasswords = myPasswords; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_recycler_layout,parent,false)); 

    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     Password myPass = getItem(position); 
     holder.recImage.setImageResource(R.drawable.ic_menu_camera); 
     holder.recTitle.setText(myPass.getTitle()); 
     holder.recPassword.setText(myPass.getPassword()); 
    } 

    @Override 
    public int getItemCount() { 
     return myPasswords.size(); 
    } 

    public Password getItem(int position){ 
     return myPasswords.get(position); 
    } 


    //swipe to delete-dismiss 
    public void dismissItem(int position){ 
     myPasswords.remove(position); 
     notifyItemRemoved(position); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     TextView recTitle,recPassword; 
     ImageView recImage; 
     public ViewHolder(View itemView) { 
      super(itemView); 
      recTitle = (TextView)itemView.findViewById(R.id.recTitle); 
      recPassword = (TextView)itemView.findViewById(R.id.recPassword); 
      recImage = (ImageView)itemView.findViewById(R.id.recImage); 
     } 
    } 

} 
+0

我只想指出SQLite和MySQL是完全不同的,MySQLite不是。 –

+0

我想感谢你@ cricket_007在我的问题和您的答案更正。在我的代码没有错误。你真的救了我! –

回答

0

我的数据库不能RecyclerAdapter

叫你为什么不能给RecyclerAdapter数据库对象?

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 
    List<Password> myPasswords; 
    MySQLiteHelper db; 

    public RecyclerAdapter(MySQLiteHelper db) { 
     this.db = db; 
     this.myPasswords = db.getAllPasswords(); 
    } 

很明显,你必须确定这种方法对代码的其他任何潜在的副作用,但它只是表明,它是可以使用RecyclerAdapter类的数据库里面

0

我认为你必须在代码本身你的答案,你基本上做到CRU(d)elete操作解决你的问题。

所以从你的刷卡类,做这样的判断:

<database variable>.deletePassword(the key you want to delete) 

而且是不添加以下notifyDataSetChange(),使屏幕刷新。

0

你在Recyclelerview中执行remle查询

//swipe to delete-dismiss 
public void dismissItem(int position){ 
    myPasswords.remove(position); 
    Here you want excute the query 
    ////deletePassword(Password password)(your query) 

    notifyItemRemoved(position); 
} 
相关问题