Home » Android » How to update the iterates in SQLite on Button press RecyclerView Android-Exceptionshub

How to update the iterates in SQLite on Button press RecyclerView Android-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have one DB named DatabaseHelper, and one RecyclerView, my problem is when I want to iterate the quantity for one product, he just works, the value remains in DB, but for example, the quantity is 1, when I press + button, he iterates on 2, and when I press the – button, he just iterates on 0, no less and no more, if I let the quantity for that object on 2 and I left the activity (back button) and I enter in the activity again, the quantity remain 2 for example and now I can iterate on 3 (at + button press) and 1 (a – button press). I want to make this iteration continue. I put my adapter and the DB class here.

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "favorites.db";
public static final String TABLE_NAME_CART = "cart_table";

public static final String COL_1 = "IMAGE";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PRICE";
public static final String COL_4 = "DETAILS";
public static final String COL_5 = "MODEL";
public static final String COL_6 = "IMAGEE";
public static final String COL_7 = "IMAGEEE";
public static final String COL_8 = "QUANTITY";

 public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

private static DatabaseHelper sDatabaseHelper;


public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}



    public static DatabaseHelper getInstance(Context context) {
    if (sDatabaseHelper == null) {
        sDatabaseHelper = new DatabaseHelper(context);
    }
    return sDatabaseHelper;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME_FAVORITES + " (" + COL_1 + " INTEGER, " + COL_2 + " VARCHAR, " + COL_3 + " INTEGER, " + COL_4 + " VARCHAR, " + COL_5 + " VARCHAR, " + COL_6 + " INTEGER, " + COL_7 + " INTEGER, " + COL_8 + " INTEGER);");
    db.execSQL("CREATE TABLE " + TABLE_NAME_CART + " (" + COL_1 + " INTEGER, " + COL_2 + " VARCHAR, " + COL_3 + " INTEGER, " + COL_4 + " VARCHAR, " + COL_5 + " VARCHAR, " + COL_6 + " INTEGER, " + COL_7 + " INTEGER, " + COL_8 + " INTEGER);");

} 
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_FAVORITES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_CART);
    onCreate(db);
}

// Insert Data in cart table
public boolean insertDataCart(items items) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, items.getlImageId());
    contentValues.put(COL_2, items.getlName());
    contentValues.put(COL_3, items.getlPrice());
    contentValues.put(COL_4, items.getlDetails());
    contentValues.put(COL_5, items.getlModel());
    contentValues.put(COL_6, items.getlImageId1());
    contentValues.put(COL_7, items.getlImageId2());
    contentValues.put(COL_8, items.getQuantity());

    long result = db.insert(TABLE_NAME_CART, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

// See Data Cart
public ArrayList<items> getAllDataCart() {
    ArrayList<items> favItem = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME_CART, null);

    while (res.moveToNext()) {
        int img = res.getInt(0);
        String name = res.getString(1);
        int price = res.getInt(2);
        String details = res.getString(3);
        String model = res.getString(4);
        int img1 = res.getInt(5);
        int img2 = res.getInt(6);
        int quantity = res.getInt(7);
        items newItems = new items(name, price, img, img1, img2, details, model,quantity);
        favItem.add(newItems);
    }
    return favItem;
}

 // Update Cart Quantity
public void updateDataCard(int id, int quantity) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues val = new ContentValues();

    val.put(COL_8, quantity);

    int result =  db.update(TABLE_NAME_CART, val, "IMAGE=" + id, null);}}

And here is my RecyclerView

public class cartItemRecyclerView extends RecyclerView.Adapter<cartItemRecyclerView.ViewHolder>{

DatabaseHelper db;

ArrayList<items> itemsList;
Context context;

interface IImageButtonListener{
    void onImageButtonClick(View view,int pos,boolean isDecrease);
}

public cartItemRecyclerView(ArrayList<items> item,DatabaseHelper db, Context context) {
    this.itemsList = item;
    this.db = db;
    this.context =  context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View contactView = inflater.inflate(R.layout.list_cart_item_view,parent,false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(contactView);

    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    // Get the data model based on position
    items itm = itemsList.get(position);

    // Set item views based on your view and data model

    holder.nameC.setText(itm.getlName());
    holder.iconC.setImageResource(itm.getlImageId());
    holder.priceC.setText(itm.getlPrice()+ "");
    holder.quantityC.setText(itm.getQuantity()+"");

    holder.setListener(new IImageButtonListener() {
        @Override
        public void onImageButtonClick(View view, int pos, boolean isDecrease) {
            int i = itemsList.get(pos).getQuantity();
            int id = itemsList.get(pos).getlImageId();

            if(isDecrease){
                i++;
            }
            else {
                i--;
            }
            holder.quantityC.setText(i+"");
            db.updateDataCard(id,i);

        }
    });

}

@Override
public int getItemCount() {
    return itemsList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView nameC, priceC, quantityC;
    public ImageView increaseC, decreaseC, iconC;
    IImageButtonListener listener;

    public void setListener(IImageButtonListener listener){
        this.listener = listener;
    }

    public ViewHolder(View itemView){
        super(itemView);

        nameC = itemView.findViewById(R.id.name_item_cart);
        priceC = itemView.findViewById(R.id.price_item_cart);
        quantityC = itemView.findViewById(R.id.textview_quantity_items);
        increaseC = itemView.findViewById(R.id.image_increase_quantity);
        decreaseC = itemView.findViewById(R.id.image_decrease_quantity);
        iconC = itemView.findViewById(R.id.icon_item_cart);

        // Event
        increaseC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImageButtonClick(v, getAdapterPosition(),true);
            }
        });

        decreaseC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImageButtonClick(v, getAdapterPosition(),false);
            }
        });
    }
}
How to&Answers: