-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add to cart via SQLite DB. Feature is incomplete.
Issue: Not able to populate data in listview in CartActivity
- Loading branch information
1 parent
65d5ca3
commit b8e2ac5
Showing
15 changed files
with
291 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.BugBazaar.ui.cart; | ||
|
||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Cart { | ||
private static Cart instance; | ||
private CartDatabaseHelper dbHelper; | ||
private SQLiteDatabase database; | ||
|
||
|
||
|
||
public long addCartItem(CartItem cartItem) { | ||
ContentValues values = new ContentValues(); | ||
values.put(CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_NAME, cartItem.getProductName()); | ||
values.put(CartItemDBModel.CartItemEntry.COLUMN_QUANTITY, cartItem.getQuantity()); | ||
values.put(CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_PRICE, cartItem.getPrice()); | ||
|
||
return database.insert(CartItemDBModel.CartItemEntry.TABLE_NAME, null, values); | ||
} | ||
|
||
public List<CartItem> getCartItems() { | ||
List<CartItem> cartItems = new ArrayList<>(); | ||
String[] projection = { | ||
CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_NAME, | ||
CartItemDBModel.CartItemEntry.COLUMN_QUANTITY, | ||
CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_PRICE | ||
}; | ||
|
||
Cursor cursor = database.query( | ||
CartItemDBModel.CartItemEntry.TABLE_NAME, | ||
projection, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null | ||
); | ||
|
||
if (cursor != null) { | ||
while (cursor.moveToNext()) { | ||
String productName = cursor.getString(cursor.getColumnIndexOrThrow(CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_NAME)); | ||
int quantity = cursor.getInt(cursor.getColumnIndexOrThrow(CartItemDBModel.CartItemEntry.COLUMN_QUANTITY)); | ||
int price = cursor.getInt(cursor.getColumnIndexOrThrow(CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_PRICE)); | ||
CartItem cartItem = new CartItem(productName, quantity, price); | ||
cartItems.add(cartItem); | ||
} | ||
cursor.close(); | ||
} | ||
|
||
return cartItems; | ||
} | ||
|
||
public void clearCart() { | ||
database.delete(CartItemDBModel.CartItemEntry.TABLE_NAME, null, null); | ||
} | ||
|
||
// Add methods for removing items, updating quantities, etc. as needed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...in/java/com/BugBazaar/ui/CartAdapter.java → ...va/com/BugBazaar/ui/cart/CartAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.BugBazaar.ui.cart; | ||
import android.content.ContentValues; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
|
||
public class CartDatabaseHelper extends SQLiteOpenHelper { | ||
private static final String DATABASE_NAME = "cart.db"; | ||
private static final int DATABASE_VERSION = 1; | ||
|
||
// SQL command to create the cart_items table | ||
private static final String SQL_CREATE_CART_TABLE = | ||
"CREATE TABLE " + CartItemDBModel.CartItemEntry.TABLE_NAME + " (" + | ||
CartItemDBModel.CartItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_NAME + " TEXT," + | ||
CartItemDBModel.CartItemEntry.COLUMN_PRODUCT_PRICE + " INTEGER," + | ||
CartItemDBModel.CartItemEntry.COLUMN_QUANTITY + " INTEGER"+ | ||
");"; | ||
|
||
// SQL command to delete the cart_items table | ||
private static final String SQL_DELETE_CART_TABLE = | ||
"DROP TABLE IF EXISTS " + CartItemDBModel.CartItemEntry.TABLE_NAME; | ||
|
||
public CartDatabaseHelper(Context context, String dbname, SQLiteDatabase.CursorFactory factory, int DATABASE_VERSION) { | ||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL(SQL_CREATE_CART_TABLE); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
db.execSQL(SQL_DELETE_CART_TABLE); | ||
onCreate(db); | ||
} | ||
|
||
public long saveProductDetails(String ProductName, int ProductPrice, int ProductQuantity) { | ||
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); | ||
ContentValues cv = new ContentValues(); | ||
cv.put("product_name", ProductName); | ||
cv.put("product_price", ProductPrice); | ||
cv.put("product_quantity", ProductQuantity); | ||
long recordid = sqLiteDatabase.insert("cart_items", null, cv); | ||
return recordid; | ||
} | ||
|
||
public List<CartItem> getAllRecords() { | ||
List<CartItem> cartItems = new ArrayList<>(); | ||
|
||
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase(); | ||
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM cart_items", null); | ||
CartItem cartItem; | ||
while (cursor.moveToNext()) { | ||
String product_name = cursor.getString(1); | ||
int product_price = cursor.getInt(2); | ||
int product_quantity = cursor.getInt(3); | ||
|
||
//Things are getting added into list. | ||
cartItem = new CartItem(product_name, product_price, product_quantity); | ||
cartItems.add(cartItem); | ||
//Log.d("DatabaseHelper", "CartItem " + cartItem.getProductName() + ", Quantity " + cartItem.getQuantity()); | ||
// Add debug logging to check retrieved values | ||
} | ||
return cartItems; | ||
} | ||
|
||
} |
Oops, something went wrong.