Padelis Theodosiou Padelis Theodosiou - 3 months ago 25
Android Question

Remove data from a SQLite database inside a RecyclerAdapter

I want to remove the data from mydatabase inside my RecyclerAdapter.

I've also got a swipe class that deletes the items from the RecyclerView.

Now, I'm able to remove the items from the RecyclerView, but not from the database as my database can't be called on the RecyclerAdapter.

This is my 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 class

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


This is my RecyclerAdapter class

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

}

Answer

my Database can't be called on RecyclerAdapter

Why can't you give the RecyclerAdapter the database object?

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

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

Obviously, you have to determine if this approach has any potential side-effects for the rest of the code, but it just shows that it is possible to use the database inside of the RecyclerAdapter class

Comments