从RecyclerAdapter中的SQLite数据库中删除数据

时间:2016-08-22 20:47:44

标签: android android-recyclerview android-sqlite recycler-adapter

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

我还有一个滑动类,可以从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);
        }
    }

}

3 个答案:

答案 0 :(得分:0)

我认为你在代码本身有答案,你基本上做CRU(D)elete操作来解决你的问题。

因此,从您的滑动课程中,拨打此电话:

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

是的,添加以下notifyDataSetChange()以便屏幕刷新。

答案 1 :(得分: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类中使用数据库

答案 2 :(得分:0)

您在适配器

中的Recyclerview中执行拒绝查询
    //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);
}