0

RecyclerAdapter 내의 mydatabase에서 데이터를 제거하려고합니다.RecyclerAdapter 내의 SQLite 데이터베이스에서 데이터 제거

또한 RecyclerView에서 항목을 삭제하는 스 와이프 클래스가 있습니다.
이제 RecyclerAdapter에서 데이터베이스를 호출 할 수 없으므로 RecyclerView에서 항목을 제거 할 수 있지만 데이터베이스에서는 항목을 제거 할 수 없습니다.

이것은 내 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

어댑터의 Recyclerview에서 detele 쿼리를 실행하십시오.

//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); 
} 
관련 문제