0 Replies - 3373 Views - Last Post: 28 May 2012 - 11:30 AM

#1 eyzf3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-July 09

problem price would not appear

Posted 28 May 2012 - 11:30 AM

I am trying to make a simple shopping cart android application.Sorry if the code seem to be too messy. I am both beginner in java and android programming. I can run this programming without error but the problem is at the product detail of each item,i cannot make the price of each item to appear and i am trying to make the total price of the items of what the user selected to appear when the user click on proceed to checkout. I get it to calculate the total price=price*quantity. Problem is that I not sure on how to write that part since I not sure how to write the program to calculate not all the items but only the items and quantity selected by the users and where to put it.

 ShoppingcartActivity.java

public class ShoppingCartActivity extends Activity {

private List<Product> mCartList;
private ProductAdapter mProductAdapter;

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppingcart);

 mCartList = ShoppingCartHelper.getCartList();

 // Make sure to clear the selections
  for(int i=0; i<mCartList.size(); i++) {
  mCartList.get(i).selected = false;
  }

 // Create the list
 final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
 mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), true);
 listViewCatalog.setAdapter(mProductAdapter);

 listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
  long id) {
  Intent productDetailsIntent = new               
   Intent(getBaseContext(),ProductDetailsActivity.class);
   productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
   startActivity(productDetailsIntent);
   }
  });

  }

 @Override
 protected void onresume() {
 super.onresume();

  // Refresh the data
 if(mProductAdapter != null) {
 mProductAdapter.notifyDataSetChanged();
 }
 }

 }

CatalogActivity.java

 public class CatalogActivity extends Activity {

 private List<Product> mProductList;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.catalog);

 // Obtain a reference to the product catalog
 mProductList = ShoppingCartHelper.getCatalog(getResources());

 // Create the list
 ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
 listViewCatalog.setAdapter(new ProductAdapter(mProductList, getLayoutInflater(),   
 false));

 listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
  long id) {
  Intent productDetailsIntent = new 
  Intent(getBaseContext(),ProductDetailsActivity.class);
  productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
  startActivity(productDetailsIntent);
  }
  });

  Button viewShoppingCart = (Button) findViewById(R.id.ButtonViewCart);
  viewShoppingCart.setonclickListener(new onclickListener() {

  @Override
  public void onclick(View v) {
  Intent viewShoppingCartIntent = new Intent(getBaseContext(),      
  ShoppingCartActivity.class);
  startActivity(viewShoppingCartIntent);
  }
  });

  }
  }


Product.java

public class Product {

public String title;
public Drawable productImage;
public String description;
public double price;
public boolean selected;

public Product(String title, Drawable productImage, String description,
double price) {
this.title = title;
this.productImage = productImage;
this.description = description;
this.price = price;
}

public void setPrice(double price)
{
     this.price = price;
}

public double getPrice()
{
    return price;
}

}

ProductAdapter.java

public class ProductAdapter extends BaseAdapter {

private List<Product> mProductList;
private LayoutInflater mInflater;
private boolean mShowQuantity;

public ProductAdapter(List<Product> list, LayoutInflater inflater, boolean 
showQuantity) {
mProductList = list;
mInflater = inflater;
mShowQuantity = showQuantity;
}

@Override
public int getCount() {
return mProductList.size();
}

@Override
public Object getItem(int position) {
return mProductList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewItem item;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
item = new ViewItem();

item.productImageView = (ImageView) convertView
  .findViewById(R.id.ImageViewItem);

item.productTitle = (TextView) convertView
  .findViewById(R.id.TextViewItem);

item.productQuantity = (TextView) convertView
 .findViewById(R.id.textViewQuantity);

convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}

Product curProduct = mProductList.get(position);

item.productImageView.setImageDrawable(curProduct.productImage);
item.productTitle.setText(curProduct.title);

 // Show the quantity in the cart or not
  if (mShowQuantity) {
  item.productQuantity.setText("Quantity: "
  + ShoppingCartHelper.getProductQuantity(curProduct));
  } else {
  // Hid the view
 item.productQuantity.setVisibility(View.GONE);
 }

 return convertView;
 }

 private class ViewItem {
 ImageView productImageView;
 TextView productTitle;
 TextView productQuantity;
 } 

 }



 ProductDetailsActivity.java

 public class ProductDetailsActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.productdetails);

 List<Product> catalog = ShoppingCartHelper.getCatalog(getResources());

 int productIndex = getIntent().getExtras().getInt(
 ShoppingCartHelper.PRODUCT_INDEX);
 final Product selectedProduct = catalog.get(productIndex);

 // Set the proper image and text
 ImageView productImageView = (ImageView) findViewById(R.id.ImageViewProduct);
 productImageView.setImageDrawable(selectedProduct.productImage);
 TextView productTitleTextView = (TextView) findViewById(R.id.TextViewProductTitle);
 productTitleTextView.setText(selectedProduct.title);
 TextView productDetailsTextView = (TextView)      
 findViewById(R.id.TextViewProductDetails);
 productDetailsTextView.setText(selectedProduct.description);

 // Update the current quantity in the cart
 TextView textViewCurrentQuantity = (TextView)   
 findViewById(R.id.textViewCurrentlyInCart);
 textViewCurrentQuantity.setText("Currently in Cart: "
 + ShoppingCartHelper.getProductQuantity(selectedProduct));

 // Save a reference to the quantity edit text
 final EditText editTextQuantity = (EditText) findViewById(R.id.editTextQuantity);

 Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart);
 addToCartButton.setonclickListener(new onclickListener() {

 @Override
 public void onclick(View v) {

 // Check to see that a valid quantity was entered
 int quantity = 0;
 try {
 quantity = Integer.parseInt(editTextQuantity.getText()
   .toString());

 if (quantity < 0) {
  Toast.makeText(getBaseContext(),
    "Please enter a quantity of 0 or higher",
    Toast.LENGTH_SHORT).show();
  return;
 }

 } catch (Exception e) {
 Toast.makeText(getBaseContext(),
   "Please enter a numeric quantity",
   Toast.LENGTH_SHORT).show();

   return;
   }

   // If we make it here, a valid quantity was entered
   ShoppingCartHelper.setQuantity(selectedProduct, quantity);

   // Close the activity
   finish();
   }
   });

   }

   }

 ShoppingCartEntry.java

  public class ShoppingCartEntry {

 private Product mProduct;
 private int mQuantity;

 public ShoppingCartEntry(Product product, int quantity) {
  mProduct = product;
  mQuantity = quantity;
  }

 public Product getProduct() {
  return mProduct;
 }

 public int getQuantity() {
  return mQuantity;
 }

 public void setQuantity(int quantity) {
  mQuantity = quantity;
 }

     }

 ShoppingCartHelper.java

  public class ShoppingCartHelper {

  public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
  private double price;
  /**
  * 
  */
  private static List<Product> catalog;
  private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product,  
  ShoppingCartEntry>();

  public void setPrice(double price)
  {
 this.price = price;
   }

   public double getPrice()
   {
 return price;
    }

    public static List<Product> getCatalog(Resources res){
   if(catalog == null) {
   catalog = new Vector<Product>();
   catalog.add(new Product("Dead or Alive", res
   .getDrawable(R.drawable.deadoralive),
   "Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
   catalog.add(new Product("Milo 3 in 1", res
    .getDrawable(R.drawable.milo),
    "Milo 3 in 1 nicest", 16.99));
    catalog.add(new Product("Watchmen", res
    .getDrawable(R.drawable.watchmen),
    "Watchmen by Alan Moore and Dave Gibbons", 14.99));
     }
  return catalog;
 }

 public static void setQuantity(Product product, int quantity) {
 // Get the current cart entry
 ShoppingCartEntry curEntry = cartMap.get(product);

 // If the quantity is zero or less, remove the products
 if(quantity <= 0) {
 if(curEntry != null)
 removeProduct(product);
 return;
 }

 // If a current cart entry doesn't exist, create one
 if(curEntry == null) {
 curEntry = new ShoppingCartEntry(product, quantity);
 cartMap.put(product, curEntry);
 return;
 }

 // Update the quantity
 curEntry.setQuantity(quantity);
 }

 public static int getProductQuantity(Product product) {
 // Get the current cart entry
 ShoppingCartEntry curEntry = cartMap.get(product);

 if(curEntry != null)
 return curEntry.getQuantity();

 return 0;
 }

  public static void removeProduct(Product product) {
  cartMap.remove(product);
  }

  public static List<Product> getCartList() {
  List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
  for(Product p : cartMap.keySet()) {
  cartList.add(p);
  }

   return cartList;
  }


  }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1