1. Check out our companion app, Forums for Android! Download from Google Play

Add selected product to cart in android

Discussion in 'Application Development' started by krishnaveni, Nov 28, 2012.

  1. krishnaveni

    krishnaveni Well-Known Member
    Thread Starter

    Dec 16, 2011
    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 activity.it is also done.

    Here i have to implement one part.

    the detailed order page have one button add to cart.here i have to click this button means the selected product is added on my cart.how 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(R.id.title)).getText().toString();
    String duration = ((TextView) view.findViewById(R.id.duration)).getText().toString();
    String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
    String thumb_image = ((ImageView) view.findViewById(R.id.list_image)).getImageMatrix().toString();

    // 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(R.id.cart);
    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(R.id.name_label);
    TextView lblCost = (TextView) findViewById(R.id.cost_label);
    TextView lblDesc = (TextView) findViewById(R.id.description_label);
    ImageView imgv = (ImageView) findViewById(R.id.image_label);
    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

    Jul 13, 2010
    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