Update - after following this tutorial, be sure to check out part 2 and also check out part 3.
Creating a Shopping Cart for Android
A common problem that arises when developing Android applications is figuring out how to pass information from Activity to Activity within the Android application. This problem can be solved in two ways. For passing small amounts of information, you can put extras in the Intent that gets passed along to create the activity. For managing a larger, more complex amount of information the use of static variables is recommended.
Through this tutorial we will create a simple shopping cart. At the end of the tutorial, there are several suggestions for improving the shopping cart. Some of these suggestions may be included in a future tutorial.
Screenshot of the finished Shopping Cart |
Listed in the graphic below is an outline of how the activities and layouts are connected together. This tutorial will feature three different activities and several different layouts. The layout files are the xml files listed in blue.
The product adapter class is used in both the CatalogActivity and the ShoppingCartActivity to adapt data for the list views.
Activites and Layouts of the Shopping Cart |
Step 1. Create the layouts for the app.
This shopping cart tutorial will involve several different layouts.
- Catalog
- Product Details
- Shopping Cart
After adding the product to your cart, you will be taken back to the Catalog, where you can view details for other products or proceed to view your shopping cart.
The Shopping Cart layout will include the option to remove items from your cart, and to "Proceed to Checkout."
Since this is an advanced tutorial, we will not focus on the details of creating these layouts. A more detailed tutorial on creating layouts can be found here.
catalog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="24dip" android:layout_margin="5dip" android:text="Product Catalog"></TextView> <ListView android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/ListViewCatalog" android:layout_width="fill_parent" android:background="#ffffff" android:clickable="true" android:cacheColorHint="#ffffff"> </ListView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:layout_gravity="right" android:id="@+id/ButtonViewCart" android:text="View Shopping Cart"></Button> </LinearLayout>
productdetails.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/LinearLayoutHeader" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageViewProduct" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/deadoralive" android:layout_margin="5dip"></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextViewProductTitle" android:layout_gravity="center" android:layout_margin="5dip" android:textSize="26dip" android:text="Dead or Alive" android:textColor="#000000"></TextView> </LinearLayout> <TextView android:layout_height="wrap_content" android:id="@+id/TextViewProductDetails" android:layout_width="fill_parent" android:layout_margin="5dip" android:layout_weight="1" android:textColor="#000000" android:text="Product description"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:layout_gravity="right" android:id="@+id/ButtonAddToCart" android:text="Add to Cart"></Button> </LinearLayout>
shoppingcart.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#ffffff"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="24dip" android:layout_margin="5dip" android:text="Shopping Cart"></TextView> <ListView android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/ListViewCatalog" android:layout_width="fill_parent" android:background="#ffffff" android:cacheColorHint="#ffffff" android:clickable="true" android:choiceMode="multipleChoice"> </ListView> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="right" android:layout_margin="5dip"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ButtonRemoveFromCart" android:text="Remove from Cart"></Button> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Proceed to Checkout"></Button> </LinearLayout> </LinearLayout>
Step 2. Create the Product Class
Everything in this app revolves around the product class. The product class does not contain any of its own methods and really is just more of a container for other objects that describe the product including a title, an image, and a description.
package com.dreamdom.tutorials.shoppingcart; import android.graphics.drawable.Drawable; 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; } }
Step 3. Create the ShoppingCartHelper class
The ShoppingCartHelper class is designed to hold information about all the products in the catalog, and all the products that are in the user's shopping cart.
In this tutorial, our catalog is hard coded in, so the list of products, images, and descriptions will always be the same. This could be fine for your app if you sell a very limited number of items, don't run into inventory issues and don't need to frequently update your catalog.
In this example, lets say that we are selling books. I took some pictures of a couple books laying around that we can use in this tutorial.
Switch by Chip Heath and Dan Heath |
Dead or Alive by Tom Clancy with Grant Blackwood |
Watchmen by Alan Moore and Dave Gibbons |
Make sure to download these images, and place them in your drawable folder. Make sure not to name the image of the book "switch" "switch.png" This is because the name is used in the generated class R, and switch is a java keyword.
For a more advanced shopping cart, you may wish to download an xml file containing catalog information, and parse out the xml information to dynamically update the catalog when the user runs the application.
ShoppingCartHelper.java
package com.dreamdom.tutorials.shoppingcart; import java.util.List; import java.util.Vector; import android.content.res.Resources; public class ShoppingCartHelper { public static final String PRODUCT_INDEX = "PRODUCT_INDEX"; private static List<Product> catalog; private static List<Product> cart; 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("Switch", res .getDrawable(R.drawable.switchbook), "Switch by Chip Heath and Dan Heath", 24.99)); catalog.add(new Product("Watchmen", res .getDrawable(R.drawable.watchmen), "Watchmen by Alan Moore and Dave Gibbons", 14.99)); } return catalog; } public static List<Product> getCart() { if(cart == null) { cart = new Vector<Product>(); } return cart; } }
By using static variables and static methods in the ShoppingCartHelper Class we allow any activity to access this catalog and shopping cart data, allowing us to easily and seamlessly access the same data in different activities in out application.
Step 4. Create the Item Layout
The Item layout represents how one product will be displayed in a list view in both the Catalog and Shopping Cart activities. It will show a thumbnail of the product, a title for the product, and optionally a checkbox.
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="fill_parent" android:id="@+id/LinearLayoutItem"> <ImageView android:layout_margin="5dip" android:id="@+id/ImageViewItem" android:layout_height="wrap_content" android:layout_width="100dip"></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:id="@+id/TextViewItem" android:textSize="26dip" android:text="Book Title" android:textColor="#000000" android:minLines="2" android:maxWidth="150dip"></TextView> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView> <CheckBox android:layout_height="wrap_content" android:layout_margin="5dip" android:id="@+id/CheckBoxSelected" android:focusable="false" android:clickable="false" android:layout_gravity="center" android:layout_width="wrap_content"></CheckBox> </LinearLayout>
Step 5. Create the ProductAdapter class
The ProductAdapter class is designed to be attached to a listview, and populate the data for the different products that will be in a list view. It uses the Item layout to populate the data in the list view.
The constructor for the ProductAdapter class also takes a boolean to determine if you want the checkbox to appear in the list or not. We only want the checkbox to appear in the Shopping Cart activity, so you can select multiple items in your cart and remove them if you wish.
Creating Adapters and using ListViews can be somewhat confusing, and I am definitely planning on doing a full detailed tutorial on that in the future.
ProductAdapter.java
package com.dreamdom.tutorials.shoppingcart; import java.util.List; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class ProductAdapter extends BaseAdapter { private List<Product> mProductList; private LayoutInflater mInflater; private boolean mShowCheckbox; public ProductAdapter(List<Product> list, LayoutInflater inflater, boolean showCheckbox) { mProductList = list; mInflater = inflater; mShowCheckbox = showCheckbox; } @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.productCheckbox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelected); convertView.setTag(item); } else { item = (ViewItem) convertView.getTag(); } Product curProduct = mProductList.get(position); item.productImageView.setImageDrawable(curProduct.productImage); item.productTitle.setText(curProduct.title); if(!mShowCheckbox) { item.productCheckbox.setVisibility(View.GONE); } else { if(curProduct.selected == true) item.productCheckbox.setChecked(true); else item.productCheckbox.setChecked(false); } return convertView; } private class ViewItem { ImageView productImageView; TextView productTitle; CheckBox productCheckbox; } }
Step 6. Create the CatalogActivity
In this step we will look at several snippets of code individually. Don't worry about where to add them to your project--the complete CatalogActivity.java file is listed at the end of the step.
The Catalog Activity will display all the available products from the catalog. The Catalog Activity uses the ShoppingCartHelper to accomplish this.
This snippet demonstrates how to get a reference to the catalog of items.
// Obtain a reference to the product catalog mProductList = ShoppingCartHelper.getCatalog(getResources());
When you click on an item in the catalog activity, we want it to take you to the ProductDetails Activity.
The ProductDetails Activity will be designed to be a generalized activity that can display information about any product. Since it is designed in this manner, it needs a way to figure out what product it should display details about. This is accomplished by passing along extras with the Intent that starts the activity.
Use Intent.putExtra to pass simple information between activities. In this case we will pass the index of the selected product.
Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailsActivity.class); productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position); startActivity(productDetailsIntent);
Listed below is the full source for the Catalog Activity.
CatalogActivity.java
package com.dreamdom.tutorials.shoppingcart; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; 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); } }); } }
Step 7. Create the ProductDetails Activity
In the product details activity we will get the intent that started the activity, and extra the "extra" information that was passed along with the Intent to determine what product to display details about.
This is how to extract the extra information out of the Intent.
int productIndex = getIntent().getExtras().getInt(ShoppingCartHelper.PRODUCT_INDEX); final Product selectedProduct = catalog.get(productIndex);
Warning!
For the sake of keeping this tutorial more straightforward and not 100 pages long we are not doing error checking in this step. In reality, a well designed program should handle cases where getExtras returns null, or when trying to get a specifically named extra returns null. In these situations a well designed app should fail gracefully. It is not always safe to assume that the Intent extras will be present.The full source for the ProductDetails Activity is listed below.
ProductDetailsActivity.java
package com.dreamdom.tutorials.shoppingcart; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class ProductDetailsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.productdetails); List<Product> catalog = ShoppingCartHelper.getCatalog(getResources()); final List<Product> cart = ShoppingCartHelper.getCart(); 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); Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart); addToCartButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { cart.add(selectedProduct); finish(); } }); // Disable the add to cart button if the item is already in the cart if(cart.contains(selectedProduct)) { addToCartButton.setEnabled(false); addToCartButton.setText("Item in Cart"); } } }
Screenshot of what the product details activity looks like in the final product. |
Step 8. Finally, create the ShoppingCart Activity
Some code snippets are highlighted below. The full source for the ShoppingCartActivity.java is listed at the end of the step.
The shopping cart activity will display all the items in the user's shopping cart. It will also display checkboxes next to the items, allowing the user to select multiple items at once and remove the items from the shopping cart if they so desire.
Just like the other activities, the ShoppingCartActivity uses the ShoppingCartHelper class to update the shopping cart
mCartList = ShoppingCartHelper.getCart();
The product flag includes a "selected" boolean that we use to determine whether a checkbox should be selected in the shopping cart or not.
The code snippet below will run when the shopping cart activity first starts, and is designed to make sure none of the items are checked.
// Make sure to clear the selections for(int i=0; i<mCartList.size(); i++) { mCartList.get(i).selected = false; }
When the "Remove from Cart" button is pressed, all the checked items will be removed from the cart.
Button removeButton = (Button) findViewById(R.id.ButtonRemoveFromCart); removeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Loop through and remove all the products that are selected // Loop backwards so that the remove works correctly for(int i=mCartList.size()-1; i>=0; i--) { if(mCartList.get(i).selected) { mCartList.remove(i); } } mProductAdapter.notifyDataSetChanged(); } });
Our layout also includes a "Proceed to Checkout" button that could have functionality added to interact with an API to process credit card transactions.
The full code for the ShoppingCartActivity is listed below.
ShoppingCartActivity.java
package com.dreamdom.tutorials.shoppingcart; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; 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.getCart(); // 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) { Product selectedProduct = mCartList.get(position); if(selectedProduct.selected == true) selectedProduct.selected = false; else selectedProduct.selected = true; mProductAdapter.notifyDataSetInvalidated(); } }); Button removeButton = (Button) findViewById(R.id.ButtonRemoveFromCart); removeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Loop through and remove all the products that are selected // Loop backwards so that the remove works correctly for(int i=mCartList.size()-1; i>=0; i--) { if(mCartList.get(i).selected) { mCartList.remove(i); } } mProductAdapter.notifyDataSetChanged(); } }); } }
Try Out the App
Remember to update the application manifest before running the app.
Once you get the app working, try going between the activities and adding items to the shopping cart, and removing them. If you have any questions or problems feel free to ask in the comments section of this post.
Another screenshot of the finished app |
Some Things to Think About
This tutorial represents an introduction to creating a shopping cart for Android. There are a number of ways that this application could be improved
- Include options to display user reviews
- Include option to handle quantities other than one
- Add more items to the catalog
- Dynamically download catalog information
- Persist shopping cart data
Concluding Notes
Remember, when you are designing a program that there are often many different ways to approach and solve the problem. There isn't always a right answer, but some approaches may be faster or more flexible. This tutorial probably doesn't include everything you want or need in a shopping cart, but use it as a starting point for creating one suited to your needs.
This tutorial was originally requested by a blog reader. If you questions of your own, or a request for a tutorial, don't hesitate to comment and ask!
Update - after following this tutorial, be sure to check out part 2.
------------------------------------------------------------
We create these tutorials in our free time. If you like what you see please consider buying us a cup of coffee so we can keep creating useful material. Click on the image below to make a donation via Paypal.
Great write up! Will do this tutorial soon. Will you be improving this shopping cart?
ReplyDeleteCheers!
Thanks for the comment Te!
ReplyDeleteI plan on revisiting the shopping cart and adding some more features, but don't have a definite date of when this will be, as I'm currently very busy.
Greetings sir, can you help me on my code. i think there is a bug on part three.. youre comment ison year 2011 and im here commenting now on 2017, im really desperate.. send me a message sir marventex@gmail.com
Deletehiii.. In your shopping cart example,i want edittext on the view Shopping cart page only,when i place,and i change, i am not able to remove from cart please help me its very urgentt...
ReplyDeleteHi Mitesh,
ReplyDeleteI guess it is about time to write a follow up tutorial on this shopping cart. I will include this feature in it.
Thanks.
when I want to change this online shopping cart (via internet) what
ReplyDeleteCan I change to each class java android.
Hey montas,
ReplyDeleteThanks for the comment. I am currently working on part two of the shopping cart tutorial, where you can edit the quantity of items.
I will then start writing a tutorial demonstrating how you can add actual payments.
Thanks for the comments! It helps me know that people are interested.
hi BusinessIsGood,
ReplyDeletei tried with your tutorial in my application for restaurant but i have some problems can you help me? :(
my problem is in the cart shopping duplicate my meal(plat) so when I cliqued in item of meal 1 and put him in cart shopping and I cliqued in item 2 of meal so, I get in cart shopping 2 item with same name.
if you like I will give you my code to help me.
A follow up tutorial has been posted. This tutorial modifies the shopping cart to handle quantities of products. Let me know if this helps.
ReplyDeleteHi is it possible to teach me how your shopping cart can be dynamically changed with an external database?
DeleteAwesome! I'll be looking forward to your payment method tutorial.
ReplyDeleteHi,
ReplyDeleteI am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon. Thanks.
Hi,
DeleteI am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon. Thanks.
Hi,I am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon.
ReplyDeleteThanks,
This comment has been removed by the author.
ReplyDeleteThanks for this tutorial. I have a problem, it will not do anything, can you post what is supposed to be in the manifest file?
ReplyDeleteThanks
Awesome! i was looking a such a tutorial, where i can get the src code for this..
ReplyDeletehi BusinessIsGood :)
ReplyDeletehow are u ? fine?
can u have any example for iphone Shopping Cart Tutorial ?
This comment has been removed by the author.
ReplyDeletewheeww.. im so confuse.. i have follow all the step correctly.. and no error occurs .. but when i run it said the program stopped unexpectedly.. i wonder what happened.. any help?
ReplyDeletebtw.. what is it means by "Remember to update the application manifest before running the app."??
ReplyDeleteand how to update?
I think this sample is Great!
ReplyDeleteI was wondering if its posible to have an options menu with each item showing a different category set of books.
I've tried to do so,but with no success (i'm new to android)..
When i changed the array of products,the previous selections gone off!
Hi! I was wondering how can I update the manifest? what do I have to put?
ReplyDeleteplease I really need this :(
hi ve u bin able to update the manifest
DeleteGreat Tutorial I had searched so many tutorials but i dint get this much of(Clear) information........
ReplyDeleteThank you
Hi, i was wondering how do i update the AndroidManifest
ReplyDeleteAndroid app manifest is:
ReplyDelete< application android:icon="@drawable/icon" android:label="@string/app_name">
< activity android:name=".CatalogActivity"
android:label="@string/app_name">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< / intent-filter>
< / activity>
< activity android:name="ProductDetailsActivity">< /activity>
< activity android:name="ShoppingCartActivity">< /activity>
< /application>
I tried to use your code directly without any modifications. But I am getting compiler errors in @override tags used by you. For example in ProductAdapter.java class, you have used extends BaseAdapter, which doesnt have a definition of getCount(). And when i use @override tag, it gives me error because of it. But when i try using SimpleAdapter instead of BaseAdapter, it gives me error in ProductAdapter Constructor.
ReplyDeleteAny assistance with this issue will be highly appreciated.
Thanks in Advance
Ashish
Ashish,
ReplyDeleteYou can simply delete the @override tags. Often times they are added by Eclipse when you autocomplete methods, and are not strictly necessary as they only provide extra information to the compiler.
You might be getting compiler errors based on what version of the JDK you are using. It is not truly overriding a method, but implementing a method from the interface adapter.
http://developer.android.com/reference/android/widget/Adapter.html
Great Tutorial!! This is exactly what we needed!!
ReplyDeletehi it's nice tutorial, i get problem, i can't combine the shopping cart to my application, i made my application catalog list with xml, can you help me how to combine that? thank you
ReplyDeleteAwesome tutorial, works great. thanks for sharing!!! :)
ReplyDeletehi,i have copied this tutorial as it is..but i am encountering following errors
ReplyDelete1- On R.id.(something) that it cannot be resolved.
2- In productdetails.xml file
android:src="@drawable/deadoralive"
is showing error
"Error: No resource found that matches the
given name (at 'src' with value
'@drawable/deadoralive')."
I am newbie to android, can any one help me out that how to resolve these issues?
I will be really very grateful for this kind favor.
Thanks for the great tut. It just what I needed for my dissertation.
ReplyDelete@ Anonymous, you need to create a drawable file in the 'res' folder to store the image of 'deadoralive' which you have to copy from here and drag it into the drawable folder.
ReplyDeleteThanks Jill :)
DeleteI am getting errors on certain public method like this one. @Override
ReplyDeletepublic int getCount() {
return mProductList.size();
}
It tells me to remove @Override. Can someone advise what might be the problem.
Hi Simtha,
DeleteIf you see a couple of comments above we give a solution. But here it is again:
You can simply delete the @override tags. Often times they are added by Eclipse when you autocomplete methods, and are not strictly necessary as they only provide extra information to the compiler.
You might be getting compiler errors based on what version of the JDK you are using. It is not truly overriding a method, but implementing a method from the interface adapter.
http://developer.android.com/reference/android/widget/Adapter.html
@jill, Thanks alot!! it resolved the issue.
ReplyDeleteI find a lot of @Override errors are due to compiling in the wrong compliance level.
ReplyDeleteUnder Project | Properties | Java Compiler
There is a "Compiler compliance level:" that defaults to 1.5
Change this to 1.6 and the @Overrides will work properly.
For my version of eclipse, i had to change the default compliance level by unchecking the "enable project specific settings" in that same window and selecting the "configure workspace settings" and changing that compliance level to 1.6 (from 1.7 which did not seem to work with android)
That caused all future projects to have the correct compliance level.
Thank you for helping answer the questions :)
DeleteThis is a very good recommendation!
ty for the tutorial. it is simple and easy to follow but i face a problem..i am able to compile and run it but the problem is the image of the books do not appear..
ReplyDeleteHi! Did you download the images of the books and place them in the correct place?
Delete"Make sure to download these images, and place them in your drawable folder. Make sure not to name the image of the book "switch" "switch.png" This is because the name is used in the generated class R, and switch is a java keyword."
Also, Jill responded to a similar question in the comments above:
"@ Anonymous, you need to create a drawable file in the 'res' folder to store the image of 'deadoralive' which you have to copy from here and drag it into the drawable folder."
Hope that helps :)
I did download few images but I change the name
Deletehi! the tutorial is just perfect. it is not hard to understand. I am able to run it but only the 1st page ( catalog page) . what ever items i click no effect. can i ask what you mean by updating the android manifest xml.
ReplyDeleteHi,
DeleteWe made a post on how to update the android manifest.
You can find the answer in this post:
http://www.androiddom.com/2012/02/how-to-modify-androidmanifest.html
thnkx a lot for replying so fast.
Deletei am a beginner in android programming. I need to create shopping cart application combined with budget. I have follow your tutorial step by step and update the manifest file and able to run without error found. The problem is no matter what I click in the first page of the application like the item or the button, it keep popping out the application has stopped unexpectedly. I am wondering what is the cause of it and how to solve it
ReplyDeleteHi Ed,
DeleteWe are going to need a little bit more details in order to help you on this one. Why don't you write us an email to theandroiddom[at]gmail[dot]com and tell us what tool are you using and which Android SDK version and then we can try pin pointing that error. We are a little bit busy these next couple of days but as soon as we have time we'll help you.
thnkz. I just send an email to you
DeleteEd, I just checked our email and we haven't received anything.
Deletereally? I have sent twice to this email theandroiddom@gmail.com
DeleteI just sent again using hotmail this time. Before that , I was using yahoo to send, perhaps gone to junk
ReplyDeleteHi Ed! We got it. Sorry for the delay. We'll get to it as soon as we can.
Deletei notice that inside the program there is double price but i never see the price of the items appear. Can I ask why? If i want to make the price of the item to appear, how to do that?
ReplyDeleteHi! We are working on part 3 of the tutorial where will address this question :).
Deletereally? around when will the part 3 appear?
DeleteHi! It should be online in the next couple days.
DeleteHey Sil
DeleteCan you please tell me how to get title of all books in shopping cart when we press proceed to checkout button
please help
thank you
Hey.. how do I calculate the total price ?
ReplyDeletehmm i also want to know how. I know normally is totalprice =+ price
DeleteHi! Thanks for your question. We are working on the tutorial part III that will answer this question. Stay tuned.
DeleteWow great stuff. Part 1 really has helped me understand what is going on with different layouts and activities. My only suggestions would be to post your code in a way that has the syntax highlighted and also post the Manifest for people who are not as familiar with android. Dont mean to complain at all because this is one of the best tutorials i have found!
ReplyDeleteNice tutorial v clear explaination. will the tutorial part III coming out this week?
ReplyDeleteHi, this tutorial is superb perfect for me i have run it but i don't want to apply on listview and i removed catalog Activity... i mean i have created different activities in this project and each activity has Add to Cart button when i click on button its performing the Add to cart functionality but after that i want to continue my Add to cart process means when i click another activity button then session shoulde be maintain...any help
Deleteanyone send me the full coding i facing some error ....
ReplyDeleteanyone send me the full coding i facing some error ....tzerkang@gmail.com
ReplyDeletehmm i have tried this and the second part but I dun get it why doesn't the price appear and I would like to ask if I want to get the list of item from shopping mall database online instead of hard coded in, what are the code that I should change and it is hard to do it?
ReplyDeletehi great guide!
ReplyDeleteany tutorial for using xml file containing catalog information for dynamic product catalog?
Hello
ReplyDeleteI have a problem in the generation of the R class, I created the drawable folder in the 'res' folder and copy the images but still does not work.
Anyone can help me
thank you
Did u change the names ? of images? The names shud b same as used in code & all in lowercase and shud b full name like image.png along with the extension
DeleteI have not changed the name of the images. It's all right
DeleteHow to add pay-pal to this ?
ReplyDeleteuse paypal sdk
DeleteHi, this tutorial is superb perfect for me i have run it but i don't want to apply on listview and i removed catalog Activity... i mean i have created different activities in this project and each activity has Add to Cart button when i click on button its performing the Add to cart functionality but after that i want to continue my Add to cart process means when i click another activity button then session shoulde be maintain...any help
ReplyDeleteDear Sir
ReplyDeleteI want to get images and product details from server... please help me how i can use your code to get this ???
Hi,
ReplyDeleteI tried this tutorial for my app, It works great. But only thing is when i log-out or close and again use the app and check out whats in cart then there are no products in the cart.
Please can you help me out with the code to retain products that are added earlier in the cart even when i use the app next time.
I need this code for my app at the earliest. Please reply soon.
Thank you so much.....you solved my problem........:)
ReplyDeletei have created drawable folder in res folder i copied d images bt still its nt working can anyone help me please..i need it urgent
ReplyDeleteY ITS SHOWING ERROR??? PLEASE HELP ME
ReplyDeletei can't download this app.....i am android trainy..
ReplyDeleteplz give link to download this aap
how to direct proceed to checkkout button to a different layout
ReplyDeletereally helpful !!
ReplyDeletegr8 work
For some new android developers, this is the androidmanifest that works...
ReplyDeleteNTB: "shoppingcart" is the project name, you should replace this with your project name
--------------------------------------------------------------------
hi BusinessIsGood,
ReplyDeletemay i know your email address so that i can get help when doing your tutorials?
my email is waisoon_vanguard@hotmail.com
with regards,
student in help
i have done exactly as you have told, and updated the android manifest, but i cannot run it,it gives error like "sorry prorgam stopped unexpectedly force close" plz help me , my emailid = haq.rakibul@gmail.com
ReplyDeletehi, i am encountering an error
ReplyDeleteandroid.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.shoppingcart/com.example.shoppingcart.ShoppingCartActivity}; have you declared this activity in your AndroidManifest.xml?
and i have mentioned it in my android manifest file.. plzz help
Love your shopping cart example. Works great but I was wondering how I can open the productdetails.xml as a dialog when a selection is made in the listviewcatalog. A dialog seems to give the repeated product details more visual transition. Thanks.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHey Im also create shopping cart example/. but i dnt know how to add items in my cart. If you dont mid. please help me to add items to cart. when i was clicking the items
ReplyDeleteDear Admin,
ReplyDeleteKindly add "update quantity" field, so that qty * cost = Bill Amount in this tutorial
Dear Admin,
ReplyDeleteThank You sir .. every thing is clearly defined.
This comment has been removed by the author.
ReplyDeletehow to retrieve the product by using sqlite or external database in webserver? Please help thanks.
ReplyDeletei have done this application but unfornately unable to open the application... i have changed the andriodmainfest but unable to open. please help thanks....
ReplyDeletewith regards
rajdeep
THANK YOU ! works like a charm !
ReplyDeletehi, how are u sir ? first i want to thank you for this great tut :)
ReplyDeletei just want to ask u what if i want to connect my shopping cart to MYSQL database which i already have for my website?
having d same prob ... i wanna connect wid my DB bt can not :(
DeleteI don't have any error in the code but when I run the application in emulator I get this error "unfortunately app has stopped"
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI want to retrieve the product list from the database which section of the code will i have to modify to suit this functionality
ReplyDeleteWeb based mlm
ReplyDeleteTop 10 Marketplace (websites) where you can buy android app source code.
ReplyDeletei copy pest all the codes correctly but still fails to run in just starting... and what to changes to be made in menifest?
ReplyDeleteinsert all the activities on ur androidmanifest.xml bro
DeleteCan you show the exact menifest file
DeleteThis comment has been removed by the author.
ReplyDeletemany thanks to u dude. this help me so much. thankkssss :D
ReplyDeletei'm new in android .. and thanks to this tutorial it's very helpful to me. i want to ask how can i get all the data in the shopping cart ? like title and qty input by the user .. and show them in other activity
ReplyDeleteThis comment has been removed by the author.
ReplyDeletecan you give me a tutorial video ? because it is easier for me, iam newer
ReplyDeleteto my email nguyenphongphu2013@gmail.com
ReplyDeletethanks
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing..
ReplyDeletewww.epulsewebinfo.com
Can u help me to implement remove from cart feature
ReplyDeleteThe remove button does not work. When I select items and press it, it just unselects those items instead of removing them. Could you please help me fix it?
ReplyDeletepls tell me how to update the android manifest and activity main
ReplyDeletethank a lot... i exactly needed this...tried many ways, but failed...hope this works
ReplyDeleteIS this app has a status of online android app ??
ReplyDeleteIS this app has a status of online android app ??
ReplyDeleteHii..!!
ReplyDeleteThis is a wonderful example , please make the code downloadable ..
Thanks in advance..!!
hey i m getting app:predexdebug error i dont know what to do
ReplyDeletecan u help ? http://stackoverflow.com/questions/32363261/update-a-listview-using-sharedpreference
ReplyDeleteThank you for this tutorial!
ReplyDeletehow to android onlineshopping application img list view
ReplyDeletegracias por la ayuda
ReplyDeleteGREAT JOB SIR,
ReplyDeleteBut can I have one REQUEST sir. Can you continue the project with the function of "PROCEED TO CHECKOUT". pLEASE I need your help. Part 4 PLsssss
I am getting error foll errors :
ReplyDelete1)Error:The content of elements must consist of well-formed character data or markup.
2)Error:Cannot read packageName from C:\Users\aditi_nayak\AndroidStudioProjects\catalog\app\src\main\AndroidManifest.xml
please help.
what about proceed to checkout code????I want that code
ReplyDeletewhat about proceed to checkout code????I want that code
ReplyDeleteSir how to replace image with Image url?
ReplyDeleteHow can I change the size of one section in the list view? There's a lot of extra space and I want to remove it.
ReplyDeletei want to create a check order page.where all the details that will be in the cart will be available...how to do that?
ReplyDeleteU jus saved me sir . . . .Thnx a lot. . . .Awesome Tutorial
ReplyDeletesir, your tutorial is very help full to me...thank you soo much..
ReplyDeletei need shippingMethod Cart how can i solve the promblem of android
ReplyDeleteHi I have used your code without making any modification and its compiled fine but the I can only see the product details, while choosing any products the app stopped working....please help me on this...
ReplyDeleteI have seen android shopping app tutorial with PayPal integration but it takes time to understand. http://bit.ly/2j7NMEQ
ReplyDeleteAbsolutely fantastic sir thankx alot
ReplyDeleteho man it really works like a charm
ReplyDeleteawesome job
thanks a lot
Check this article about Android shopping app - https://www.cleveroad.com/blog/practical-tutorial-on-how-to-make-a-shopping-app
ReplyDeleteHow to checkout procced??
ReplyDeletetq
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThanks for the Tutorial..Its A Great Help
ReplyDeleteGood tutorial. Thanks a lot. One problem I encountered is with removing items from shopping cart. So I used list view multiple selection and solved my issue.This link helped me to solve the issue:
ReplyDeletehttp://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/ DivyaVipin