Skip to content

Commit

Permalink
Add to cart via SQLite DB. Feature is incomplete.
Browse files Browse the repository at this point in the history
Issue: Not able to populate data in listview in CartActivity
  • Loading branch information
banditVedant committed Sep 1, 2023
1 parent 65d5ca3 commit b8e2ac5
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 103 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
tools:replace="android:fullBackupContent"
tools:targetApi="31" >
<activity
android:name=".ui.CartActivity"
android:name=".ui.cart.CartActivity"
android:exported="false" />
<activity
android:name=".ui.Cart"
android:name=".ui.cart.Cart"
android:exported="false" />

<provider
Expand Down
30 changes: 0 additions & 30 deletions app/src/main/java/com/BugBazaar/ui/Cart.java

This file was deleted.

60 changes: 33 additions & 27 deletions app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,74 @@
import android.widget.Toast;

import com.BugBazaar.R;
import com.BugBazaar.ui.Product;
import com.BugBazaar.ui.cart.Cart;
import com.BugBazaar.ui.cart.CartActivity;
import com.BugBazaar.ui.cart.CartDatabaseHelper;
import com.BugBazaar.ui.cart.CartItem;

import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;

public class DetailedProductActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_product);

//Toolbar title set
CartDatabaseHelper cartDBHelper=new CartDatabaseHelper(this,"cart.db",null,1);

// Toolbar title set
TextView toolbarTitle = findViewById(R.id.toolbarTitle);
toolbarTitle.setText("Product Details");

// Retrieve the product details passed from the adapter
//Intent intent = getIntent();
Product product = getIntent().getParcelableExtra("product");

// Use the product details to display the detailed information
ImageView detailedImage = findViewById(R.id.detailedImage);
TextView detailedName = findViewById(R.id.detailedName);
TextView detailedDescription = findViewById(R.id.detailedDescription);
TextView detailedPrice=findViewById(R.id.detailedPrice);
TextView detailedPrice = findViewById(R.id.detailedPrice);

detailedImage.setImageResource(product.getImageResId());
detailedName.setText(product.getName());
detailedDescription.setText(product.getDescription());
detailedPrice.setText(product.getPrice());
//Add to cart button view

// Add to cart button view
Button addToCartButton = findViewById(R.id.addToCartButton);

// Handle "Add to Cart" button click
addToCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CartItem cartItem = new CartItem(product, 1);
// cartItem=getIntent().getParcelableExtra("product");
if(getIntent().getParcelableExtra("product")==null){

Log.d("amitcool", String.valueOf(cartItem));
}


// Create a CartItem instance with the clicked product and quantity 1
//CartItem cartItem = new CartItem(product, 1);
Intent intent =new Intent(getApplicationContext(), CartActivity.class);
intent.putExtra("addedCartItem",cartItem);

// Add the cartItem to the cart
Cart.getInstance().addCartItem(cartItem);
String productName = product.getName();
int productPrice = Integer.parseInt(product.getPrice());
int quantity = 1;

// Save the product details to the SQLite database
long recordId = cartDBHelper.saveProductDetails(productName, productPrice, quantity);
// Optionally, show a toast or a message to indicate the item was added to the cart
Toast.makeText(DetailedProductActivity.this, "Product has been added to cart", Toast.LENGTH_SHORT).show();

List<CartItem> cartItems = cartDBHelper.getAllRecords();
ArrayList<CartItem> cartItemList=new ArrayList<>(cartItems);
for (CartItem cartItem : cartItemList) {
Log.d("DetailedProd", "Product Name: " + cartItem.getProductName());
}
Intent intent=new Intent(DetailedProductActivity.this, CartActivity.class);
intent.putParcelableArrayListExtra("cartItems",cartItemList);
startActivity(intent);

//Optionally, show a toast or a message to indicate the item was added to the cart
Toast.makeText(DetailedProductActivity.this, "Product has been added to cart", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}


}

//Code to handle backbutton
// Code to handle back button
public void onBackButtonClick(View view) {
onBackPressed(); // Navigate back to the previous activity
}

}

20 changes: 10 additions & 10 deletions app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

import com.BugBazaar.R;
import com.BugBazaar.ui.cart.CartActivity;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
Expand Down Expand Up @@ -56,15 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {

// product data
productList = new ArrayList<>();
productList.add(new Product("Old Town Camera",getString(R.string.desc_cycle), R.drawable.item_camera,"₹3,000"));
productList.add(new Product("Dumb Watch", getString(R.string.desc_cycle), R.drawable.item_watch,"₹2,400"));
productList.add(new Product("Skate-Board", getString(R.string.desc_cycle), R.drawable.item_skateboard,"₹5,640"));
productList.add(new Product("A Lazy BiCycle", getString(R.string.desc_cycle), R.drawable.item_cycle,"₹19,000"));
productList.add(new Product("PineApple iPhone", getString(R.string.desc_cycle), R.drawable.item_iphone,"₹69,000"));
productList.add(new Product("Z Box Gaming Controller", getString(R.string.desc_cycle), R.drawable.item_gc,"₹3,400"));
productList.add(new Product("A Rat", getString(R.string.desc_cycle), R.drawable.item_mouse,"₹1,200"));
productList.add(new Product("Spy TWS", getString(R.string.desc_cycle), R.drawable.item_tws,"₹4,200"));
productList.add(new Product("VR device", getString(R.string.desc_cycle), R.drawable.item_vr,"₹24,000"));
productList.add(new Product("Old Town Camera",getString(R.string.desc_cycle), R.drawable.item_camera,"3000"));
productList.add(new Product("Dumb Watch", getString(R.string.desc_cycle), R.drawable.item_watch,"2400"));
productList.add(new Product("Skate-Board", getString(R.string.desc_cycle), R.drawable.item_skateboard,"5640"));
productList.add(new Product("A Lazy BiCycle", getString(R.string.desc_cycle), R.drawable.item_cycle,"19000"));
productList.add(new Product("PineApple iPhone", getString(R.string.desc_cycle), R.drawable.item_iphone,"69000"));
productList.add(new Product("Z Box Gaming Controller", getString(R.string.desc_cycle), R.drawable.item_gc,"3400"));
productList.add(new Product("A Rat", getString(R.string.desc_cycle), R.drawable.item_mouse,"1200"));
productList.add(new Product("Spy TWS", getString(R.string.desc_cycle), R.drawable.item_tws,"4200"));
productList.add(new Product("VR device", getString(R.string.desc_cycle), R.drawable.item_vr,"24000"));


// Create and set the adapter for the GridView
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/java/com/BugBazaar/ui/cart/Cart.java
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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.BugBazaar.ui;
package com.BugBazaar.ui.cart;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand Down Expand Up @@ -34,25 +34,24 @@ protected void onCreate(Bundle savedInstanceState) {

// Initialize your cartItems list and populate it
cartItems = new ArrayList<>();
//Log.d("addedCartItem", getIntent().getParcelableExtra("addedCartItem"));
// Retrieve the added cart item from the intent
CartItem addedCartItem = getIntent().getParcelableExtra("addedCartItem");
if(addedCartItem == null){
Log.d("nulladdedCartItem","addedCartItem is null");
}
// Add the added cart item to the list
cartItems.add(addedCartItem);

// Retrieve the ArrayList<CartItem> extra from the intent
Intent intent = getIntent();
ArrayList<CartItem> receivedCartItems = intent.getParcelableArrayListExtra("cartItems");

if (receivedCartItems != null) {
cartItems.addAll(receivedCartItems);

}

// Create and set up the adapter
cartAdapter = new CartAdapter(this, cartItems);
cartRecyclerView.setAdapter(cartAdapter);
cartRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Notify the adapter that the dataset has changed
cartAdapter.notifyDataSetChanged();
}

// ... other code ...
//Toolbar title set
//Code to handle backbutton
public void onBackButtonClick(View view) {
onBackPressed(); // Navigate back to the previous activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.BugBazaar.ui;
package com.BugBazaar.ui.cart;

import android.content.Context;
import android.view.LayoutInflater;
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java
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;
}

}
Loading

0 comments on commit b8e2ac5

Please sign in to comment.