'Swipe to delete a cardview from recyclerview (with database)
I am currently trying to have a swipe to delete function for my cardview. I am using sqlLite as the database. What I am looking for is when I swipe to delete it removes it from the app screen as well as the database. Unfortunately I am running into some issues and do not know how to implement this correctly with an arrayList of two items since all the research I have done has only showed it with arrayList of one item.
Here is my DbHelper:
private static final String TABLE_NAME = "nutrition_table";
private static final String COL1 = "ID";
private static final String COL2 = "food";
private static final String COL3 = "calories";
DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase DB) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT, " + COL3 + " TEXT)";
DB.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase DB, int i, int i1) {
DB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(DB);
}
public boolean insertData(String food, String calories) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("food", food);
contentValues.put("calories",calories);
long res = db.insert(TABLE_NAME, null, contentValues);
if (res == -1) {
return false;
} else {
return true;
}
}
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return cursor;
}
public int addCalories() {
SQLiteDatabase db = this.getWritableDatabase();
int total = 0;
Cursor cursor = db.rawQuery("SELECT SUM(" + COL3 + ") FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
total = cursor.getInt(0);
}
return total;
}
My adapter class:
private Context ctx;
private ArrayList food_id, calorie_id;
public MyAdapter(Context ctx, ArrayList food_id, ArrayList calorie_id) {
this.ctx = ctx;
this.food_id = food_id;
this.calorie_id = calorie_id;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.userentry, parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.food_id.setText(String.valueOf(food_id.get(position)));
holder.calorie_id.setText(String.valueOf(calorie_id.get(position)));
}
@Override
public int getItemCount() {
return food_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView food_id, calorie_id;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
food_id = itemView.findViewById(R.id.textFood);
calorie_id = itemView.findViewById(R.id.textCals);
}
}
}
Finally the class in which the cardview is displayed:
private Context ctx;
private ArrayList food_id, calorie_id;
public MyAdapter(Context ctx, ArrayList food_id, ArrayList calorie_id) {
this.ctx = ctx;
this.food_id = food_id;
this.calorie_id = calorie_id;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.userentry, parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.food_id.setText(String.valueOf(food_id.get(position)));
holder.calorie_id.setText(String.valueOf(calorie_id.get(position)));
}
@Override
public int getItemCount() {
return food_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView food_id, calorie_id;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
food_id = itemView.findViewById(R.id.textFood);
calorie_id = itemView.findViewById(R.id.textCals);
}
}
}
Anything helps!
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
