Android's imitation Taobao shopping cart Demo sample _android

Source: Internet
Author: User
Tags sqlite database stub

This example describes the Android implementation of the imitation Taobao shopping cart. Share to everyone for your reference, specific as follows:

Summer's enthusiasm gradually receded, autumn as scheduled and to, harvest season, small series continue to practice the road, walk, went to the shopping cart, visited Taobao or Beijing east of the small partners know shopping cart inside the baby can more than one, for love shopping girl, shopping cart inside of the goods are full, add not to go in, When I used to go shopping taobao, small series did not think how to realize the shopping cart, know where to go, but now is not the same, small make up as a developer, think is how to achieve, two days of time, with ListView to achieve, already have a model, Now small make up a simple summary of the realization of the journey of the shopping cart, to help the small partners in need, welcome to the small friends message exchange.

First of all, a simple introduction to the Listview,listview control can display items using four different views. This control allows you to make a column with or without column headers and to display accompanying icons and text. You can use the ListView control to organize list entries called ListItem objects into one of the following four different views: 1. Large (standard) icon 2 small icon 3. List 4. Report View property determines which view the control uses in the list to display items. You can also use the Labelwrap property to control whether the labels associated with the item in the list are wrapped. In addition, you can manage the sorting methods for items in the list and the appearance of the selected items. Today, small knitting and small partners to share, how to use ListView to achieve the function of shopping. Android partners know that an XML corresponds to a Java class, but a shopping cart is a bit different, because there may be more than one item in the product, so we need to have two XML, two Java classes, corresponding to the need for an adapter adapter, a model, The following small series to detail the implementation of the shopping cart process.

The first step is to write the model layer, similar to the entity layer we have written before, as shown in the following code:

/*** * Description: Shopping Cart Related information * Author: Ding * Time: August 10, 2015 09:41:18 * * Package jczb.shoping.model;
Import java.io.Serializable;
Import java.util.ArrayList;
Import java.util.List; Import Android.
r.string;
    public class ShoppingCart implements Serializable {private String proimg;
    Private String Proname;
    Private String Shopprice;
    Private String Markprice;
    Private String Procount;
    Public String getproimg () {return proimg;
    } public void Setproimg (String proimg) {this.proimg = proimg;
    Public String Getproname () {return proname;
    } public void Setproname (String proname) {proname = Proname;
    Public String Getshopprice () {return shopprice;
    } public void Setshopprice (String shopprice) {this.shopprice = Shopprice;
    Public String Getmarkprice () {return markprice;
    } public void Setmarkprice (String markprice) {this.markprice = Markprice; } public String Getprocount() {return procount;
    } public void Setprocount (String procount) {this.procount = Procount;

 }
}

Step two, we write the XML file, we need to write two XML files, first we write the Activity_shoppingcart.xml file, the code looks like this:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" "Vertical" &
  Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "50DP" android:background= "#438F CB "android:orientation=" Horizontal > <!--angle bracket layout--> <imageview android:layout_width= "
        0DP "android:layout_height=" Match_parent "android:layout_weight=" 1 "android:padding=" 8DP " android:src= "@drawable/tb_icon_actionbar_back"/> <!--shopping cart layout--> <textview android:layout_wi
       Dth= "0DP" android:layout_height= "match_parent" android:layout_weight= "5.49" android:gravity= "center" 
     android:text= "Shopping Cart" android:textcolor= "#FFFFFF" android:textsize= "20sp"/> <!--edit Layout--> <textview andRoid:layout_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "3.18" Android:gra vity= "Center" android:text= "edit" android:textcolor= "#FFFFFF" android:textsize= "20sp"/> </line Arlayout> <!--ListView, there are probably more things in the shopping cart that need listview for display--> <linearlayout android:layout_width= "fi Ll_parent "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:orientation=" ver
       Tical "android:layout_margintop=" 0DP "> <listview android:id=" @+id/cart_shopping_listview " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:divider= "#808080" an droid:dividerheight= "0.5DP" > </ListView> </LinearLayout> <linearlayout android:layout_width= "Match_parent" android:layout_height= "50DP" android:layout_alignparentbottom= "true" android:orientation= "Horiz Ontal "> <!--Full-selection layout--> <checkbox android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:text= "Select All"/> <!--total layout--> <textview Andro Id:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "Right" android:paddingright= "10DP" android:textcolor= "#F63A19" android:text= "total: ¥88"/> <! --to settle the layout--> <textview android:id= "@+id/jiesuan_button" android:layout_width= "80DP" Androi d:layout_height= "Wrap_content" android:layout_marginright= "10DP" android:background= "@drawable/android_login _color "android:gravity=" center "android:padding=" 10DP "android:text=" settlement "/> </linearlayout&gt
;

 </linearlayout >

Let's look at the effect of the XML layout as shown in the following illustration:

Then we'll lay out the second Xml,activity_shoppingcart_item.xml, and the code looks like this:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" "Vertical" &
  Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation= "Horizontal" > <!--small hook layout--> <checkbox android:id= "@+id/pro_checkbox" Android:layout_wid Th= "Wrap_content" android:layout_height= "Wrap_content" android:focusable= "false" Android:focusableintouc Hmode= "false"/> <!--picture layout--> <imageview android:id= "@+id/pro_image" Android:layout_w
       Idth= "80DP" android:layout_height= "80DP" android:layout_margin= "5DP" android:scaletype= "Centercrop" android:src= "@drawable/detail_show_1"/> <!--the layout of commodity names and prices--> <linearlayout android:layou
       T_width= "Fill_parent"android:layout_height= "wrap_content" android:orientation= "vertical" > <!--the layout of commodity names--> Xtview android:id= "@+id/pro_name" android:layout_width= wrap_content "android:layout_height=" WR Ap_content "android:layout_margintop=" 10DP "android:text=" Dress female Summer "/> <!--price layout > <linearlayout android:layout_width= "match_parent" android:layout_height= "33DP" a ndroid:orientation= "Horizontal" > <textview android:id= "@+id/pro_shopprice" Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Bottom" an droid:layout_margintop= "10DP" android:text= "¥88" android:textsize= "16sp"/> <!--<text View android:id= "@+id/pro_markprice" android:layout_width= "Wrap_content" Android:layout_heigh
       t= "Wrap_content"    android:layout_gravity= "Bottom" android:layout_margintop= "10DP" android:text= "¥66" an Droid:textsize= "16sp"/>--> </LinearLayout> <linearlayout android:layout_width= "150
         DP "android:layout_height=" 33DP "android:orientation=" Horizontal "> <!--plus--> <button android:id= "@+id/pro_add" android:layout_width= "Wrap_content" Android:layo
           ut_height= "34DP" android:text= "+"/> <textview android:id= "@+id/pro_count" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "bo
           Ttom "android:layout_margintop=" 10DP "android:text=" android:textsize= "13sp"/> <!--minus--> <button android:id= "@+id/pro_reduce" android:layout_width= "wrap" _content "android:layout_height= "34DP" android:layout_marginright= "0DP" android:text= "-"/> </line

 arlayout> </LinearLayout> </LinearLayout> </LinearLayout>

The layout effect looks like this:

The third step, we write the code in the adapter adapter, that is, Shoppingcartadapter, the specific code is as follows:

Package jczb.shoping.adapter;
Import java.util.List;
Import CN.JPUSH.ANDROID.DATA.R;
Import Jczb.shoping.adapter.productsListAdapter.ViewHolder;
Import jczb.shoping.adapter.productsListAdapter.searchList;
Import Jczb.shoping.model.productSonSorting_cate;
Import Jczb.shoping.model.shoppingCart;
Import jczb.shoping.model.sonSortigns;
Import JCZB.SHOPING.UI.R;
Import jczb.shoping.ui.ShoppingCartActivity;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.CheckBox;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
  public class Shoppingcartadapter extends Baseadapter {private context mcontext;
  Private list<shoppingcart> mlist; Public Shoppingcartadapter (ContExt mcontext,list<shoppingcart> mlist) {super ();
    This.mcontext = Mcontext;
    This.mlist = mlist;
    @Override public int GetCount () {//TODO auto-generated Method stub if (mlist==null) {return 0;
    }else {return this.mList.size ();
      @Override public Object getitem (int position) {//TODO auto-generated Method Stub if (mlist = = null) {
    return null;
    else {return this.mList.get (position);
  @Override public long getitemid (int position) {//TODO auto-generated a stub return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO auto-generated method
    Stub viewholder holder = null;
  if (Convertview = = null) {holder = new Viewholder ();
      Convertview = Layoutinflater.from (This.mcontext). Inflate (R.layout.activity_shoppingcart_item, null,true); Holder.image= (ImageView) Convertview.findviewbyid (r.id.pro_image);
      Holder.chose= (CheckBox) Convertview.findviewbyid (R.id.pro_checkbox);
      Holder.proname= (TextView) Convertview.findviewbyid (r.id.pro_name);
      Holder.proprice= (TextView) Convertview.findviewbyid (R.id.pro_shopprice);
      Holder.procount= (TextView) Convertview.findviewbyid (R.id.pro_count);
    Convertview.settag (holder);
    else {holder = (Viewholder) convertview.gettag ();
      } if (this.mlist!= null) {ShoppingCart shoppinglist=this.mlist.get (position);
      Holder.proName.setText (Shoppinglist.getproname (). toString ());
      Holder.proPrice.setText (Shoppinglist.getshopprice (). toString ());
    Holder.proCount.setText (Shoppinglist.getprocount (). toString ());
  return convertview;
    /* Define Item Object/public class Viewholder {ImageView image;
    TextView Proname;
    CheckBox chose;
    TextView Proprice;
 TextView Procount;

 }
}

The fourth step, write the code inside the Java class, we first write the content in the Shoppingcartitemactivity.java, the specific code as follows:

Package jczb.shoping.ui;
Import android.app.Activity;
Import Android.os.Bundle;
public class Shoppingcartitemactivity extends activity {
  protected void onCreate (Bundle savedinstancestate) {
    Super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_shoppingcart_item);
 }


Step fifth, write the contents of the Shoppingcartactivity.java, as follows:

Package jczb.shoping.ui;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Jczb.shoping.adapter.ShoppingCartAdapter;
Import Jczb.shoping.common.AgentApi;
Import Jczb.shoping.model.shoppingCart;
Import Jczb.shoping.ui.SearchActivity.ViewHolder;
Import Jczb.shoping.ui.ShoppingcartActivity2.myThread;
Import Com.alibaba.fastjson.JSON; Import Android.
r.string;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
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.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
  public class Shoppingcartactivity extends activity{TextView jiesuan,proname,shopprice,procount;
  ListView Alistview;
  Private Layoutinflater Layoutinflater;
private TextView name;    protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_shoppingcart);
    Findviewbyid (); /* Start thread/new Thread (new Mythread ()). Start ();
    {}/* Find control based on ID/public void Findviewbyid () {alistview= (ListView) Findviewbyid (R.id.cart_shopping_listview); //Open Thread public class Mythread implements Runnable {public void run () {msg = new message
        ();
          try {map<string, string> parmas = new hashmap<string, string> ();
          Parmas.put ("username", "1");
    Parmas.put ("Password", "2");
          String url = "Http://192.168.1.110:8080/SchoolShopJson/ShoppingCart.txt";
 The data to be sent and the address of the access String result = Agentapi.dopost (parmas, URL);
          If the returned null or the IP address entered when initializing is invalid (returns the following string), the server connection failed!
          if (result = = NULL) {//Use-1 represents server connection Failure msg.what =-1;
            else {msg.what=1; Msg.oBj=result;
          } catch (Exception e) {e.printstacktrace ();
          Use-1 to represent the program exception msg.what =-2;
        Msg.obj = e;
      Mhandler.sendmessage (msg); } protected void Initview () {//TODO auto-generated Method stub}/* Child thread-parse data/private Hand
          Ler Mhandler = new Handler () {public void Handlemessage (msg) {switch (msg.what) {case-1:
          Toast.maketext (shoppingcartactivity.this, "Server Connection Failed!", Toast.length_short). Show ();
        Break
          Case-2: Toast.maketext (Shoppingcartactivity.this, "oops, wrong ...", Toast.length_short). Show ();
        Break
          Case 1:string temp = (String) msg.obj;
    Converts the obtained JSON to an array of list<shoppingcart> Shoppingcartinfo = Json.parsearray (Temp,shoppingcart.class);
          Listview.setadapter (New Shoppingcartadapter (Shoppingcartactivity.this, shoppingcartinfo)); BreAk
        Default:break;
}
      }
    };

 }

Let's take a look at the effect of the operation, as follows:

More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View Overview", "The activity of Android programming skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Development Primer and Advanced tutorials, Android Resource operations tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.