Home > Blockchain >  Android - Get recyclerview clicked item's SQLite object data
Android - Get recyclerview clicked item's SQLite object data

Time:12-31

I want to delete a clicked item of a RecyclerView where the items consist of data from SQLite.

I've successfully detected a clicked item for the RecyclerView (followed a tutorial from https://www.youtube.com/watch?v=69C1ljfDvl0&t=487s) but cannot get the item's data to be deleted by a delete method I already created. Here is the delete method:

public boolean deleteOne(OneTimeActivityModel oneTimeActivityModel){
        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "DELETE FROM "   ONE_TIME_ACTIVITY_TABLE   " WHERE "   COLUMN_ACTIVITY_NAME   " = "   oneTimeModel.getId();

        Cursor cursor = db.rawQuery(queryString, null);

        if(cursor.moveToFirst()){
            return true;
        }
        else{
            return false;
        }
    }

The method works in ListView because in ListView I can use .getItemAtPosition(position) when the item is clicked and get the model of the item to be sent to the method.

Here is the code where the item is clicked:

@Override
    public void onNoteClick(int position) {
        OneTimeDatabaseHelper oneTimeDatabaseHelper = new OneTimeDatabaseHelper(ListActivity.this);
        OneTimeActivityModel clickedActivity; // How to make this variable become the model of the clicked item

        // in ListView I can use this following line
        // OneTimeActivityModel clickedActivity = (OneTimeActivityModel) parent.getItemAtPosition(position);

        oneTimeDatabaseHelper.deleteOne(clickedActivity); //To be sent to the delete method

    }

It seems that I need to store the item in the "oneTimeDatabaseHelper" variable to be sent to the method.

How do I get the datas of the clicked item in RecyclerView?

CodePudding user response:

I believe getting the whole model is not necessary in this case. Rather than taking the whole OneTimeActivityModel as an argument, you can take just the ID or any unique identifiers in order to be used as the deletion criteria in the WHERE SQL clause.

It can be rewritten like this:

public boolean deleteOne(String oneTimeID){
        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "DELETE FROM "   ONE_TIME_ACTIVITY_TABLE   " WHERE "   COLUMN_ACTIVITY_NAME   " = "   "'"   oneTimeID   "'";

        Cursor cursor = db.rawQuery(queryString, null);

        if(cursor.moveToFirst()){
            return true;
        }
        else{
            return false;
        }
    }

As for the second question, you can take the ID of the currently clicked item, and then you pass that corresponding ID into oneTimeDatabaseHelper.deleteOne. To provide a basis to rely on:

@Override
    public void onNoteClick(int position) {
        OneTimeDatabaseHelper oneTimeDatabaseHelper = new OneTimeDatabaseHelper(ListActivity.this);
        OneTimeActivityModel clickedActivity = dataSource.get(position);
        oneTimeDatabaseHelper.deleteOne(clickedActivity.getID());
        adapter.notifyDataSetChanged();
    }

Despite my answer, if I were you, I would inject the model at the onBindViewHolder method, and process each and every model at the ViewHolder level instead of brute-forcing via onClick in the general adapter class. For example:

@Override
public void onBindViewHolder(ViewHolder holder, int position, OneTimeActivityModel model) {
    holder.bindObject(model);
}

While in the ViewHolder, you'll do something like:

// omitted for brevity
public void bindObject(OneTimeActivityModel model) {
    view.setOnClickListener(...) // do whatever you want here.
}

One advantage of my approach over yours is that the models are strongly typed and you do not have to call notifyDataSetChanged and/or get your data source like that (using position while you can just fetch it from a holder).

  • Related