Add selected product to cart in android

Last Updated:

  1. krishnaveni

    krishnaveni Well-Known Member

    I have developed one android list view app using xml parsing.this is done for my first page.

    After i have to click any product from list means am getting the detailed product description in displayed on next is also done.

    Here i have to implement one part.

    the detailed order page have one button add to i have to click this button means the selected product is added on my can i implement this.please help me.

    This is my first activity(list view using xml parsing):

    public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "";
    // XML node keys
    static final String KEY_SONG = "Product"; // parent node
    static final String KEY_ID = "productid";
    static final String KEY_TITLE = "Name";
    static final String KEY_ARTIST = "ProductURL";
    static final String KEY_DURATION = "Price";
    static final String KEY_THUMB_URL = "Image";

    ListView list;
    LazyAdapter adapter;

    public void onCreate(Bundle savedInstanceState) {

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);

    // looping through all song nodes &lt;song&gt;
    for (int i = 0; i < nl.getLength(); i++) {
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);
    // adding each child node to HashMap key =&gt; value
    map.put(KEY_ID, parser.getValue(e, KEY_ID));
    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
    map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

    // adding HashList to ArrayList


    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {

    String title = ((TextView) view.findViewById(;
    String duration = ((TextView) view.findViewById(;
    String artist = ((TextView) view.findViewById(;
    String thumb_image = ((ImageView) view.findViewById(;

    // Starting new intent
    Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    in.putExtra(KEY_TITLE, title);
    in.putExtra(KEY_DURATION, duration);
    in.putExtra(KEY_ARTIST, artist);
    in.putExtra(KEY_THUMB_URL, thumb_image);


    This is detailed description activity:

    public class SingleMenuItemActivity extends Activity {
    Button cart;
    // XML node keys
    static final String KEY_TITLE = "Name";
    static final String KEY_DURATION = "Price";
    static final String KEY_ARTIST = "ProductURL";
    static final String KEY_THUMB_URL = "Image";

    public void onCreate(Bundle savedInstanceState) {

    int productIndex = getIntent().getExtras().getInt(CustomizedListView.URL);

    //final CustomizedListView selectedProduct = songsList.getInt(productIndex);
    cart = (Button)findViewById(;
    cart.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    // getting intent data
    Intent in = getIntent();

    // Get XML values from previous intent
    String title = in.getStringExtra(KEY_TITLE);
    String duration = in.getStringExtra(KEY_DURATION);
    String artist = in.getStringExtra(KEY_ARTIST);
    final String thumb_image = in.getStringExtra(KEY_THUMB_URL);
    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(;
    TextView lblCost = (TextView) findViewById(;
    TextView lblDesc = (TextView) findViewById(;
    ImageView imgv = (ImageView) findViewById(;
    ImageLoader imageLoader = new ImageLoader(getApplicationContext());

    imageLoader.DisplayImage(thumb_image, imgv);

    I have to add add to cart button in these 2nd activity.what condition i have to put here for selected product item is add to cart .please give me solution for some useful coding wise.please help me.

  2. jonbonazza

    jonbonazza Well-Known Member

    First of all, the datastore shouldn't exist locally. Doing this would create a HUGE security vulnerability, as well as allow for easy hacking. The datastore should be located remotely on a server somewhere, where the user would request the data from a remote web service and that web service would process this request and, if valid, push the requested data back to the client. The client would then receive the response and apply further processing.

    Also, a relational database would be more appropriate here than XML.

Share This Page