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 Databaseクラス
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());
}
}
これは、私はあなたが、基本的にCRU(D)elete操作を行う、あなたはコード自体にあなたの答えを持っていると思う
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);
}
}
}
私はSQLiteとMySQLが完全に異なっており、MySQLiteは事実ではないことを指摘したいと思います。 –
私の質問とあなたの答えの訂正のために@ cricket_007に感謝します。私のコードに誤りはありません。あなたは本当に私を救った! –