SoFunction
Updated on 2025-03-11

Android implements simple shopping cart function

This article shares the specific code for Android to implement shopping cart function for your reference. The specific content is as follows

MainActivity Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <LinearLayout
    android:
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#E24146"
    android:orientation="vertical" >
    <TextView
      android:
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:minHeight="48dp"
      android:text="Shopping cart"
      android:textColor="#ffffff"
      android:textSize="17sp" />
  </LinearLayout>

  <ListView
    android:
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:childIndicator="@null"
    android:groupIndicator="@null" >
  </ListView>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2.5"
      android:gravity="center_vertical"
      android:orientation="horizontal" >

      <CheckBox
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="4dp"
        android:button="@drawable/check_box_bg"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:gravity="center"
        android:minHeight="64dp"
        android:paddingLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="visible" />

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="total:"
        android:textSize="16sp"
        android:textStyle="bold" />

      <TextView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="¥0.00"
        android:textColor="@color/purple"
        android:textSize="16sp"
        android:textStyle="bold" />
    </LinearLayout>

    <TextView
      android:
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@color/orange"
      android:clickable="true"
      android:gravity="center"
      android:text="delete"
      android:textColor="#FAFAFA" />

    <TextView
      android:
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="#E24146"
      android:clickable="true"
      android:gravity="center"
      android:text="Payment(0)"
      android:textColor="#FAFAFA" />
  </LinearLayout>

</LinearLayout>

import ;
import .;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity implements  ,{

  private ListView listView;
  private CheckBox cb_check_all;
  private TextView tv_total_price;
  private TextView tv_delete;
  private TextView tv_go_to_pay;

  private CartAdapter adapter;

  private double totalPrice = 0.00;
  private int totalCount = 0;
  private List<HashMap<String,String>> goodsList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    initDate();
  }

  //Control price display  private void priceControl(Map<String, Integer> pitchOnMap){
    totalCount = 0;
    totalPrice = 0.00;
    for(int i=0;i<();i++){
      if(((i).get("id"))==1){
        totalCount=totalCount+((i).get("count"));
        double goodsPrice=((i).get("count"))*((i).get("price"));
        totalPrice=totalPrice+goodsPrice;
      }
    }
    tv_total_price.setText("¥ "+totalPrice);
    tv_go_to_pay.setText("Payment("+totalCount+")");
  }

  @Override
  public void refreshPrice(Map<String, Integer> pitchOnMap) {
    priceControl(pitchOnMap);
  }

  @Override
  public void onClick(View view) {
    switch (()){
      case .all_chekbox:
        AllTheSelected();
        break;
      case .tv_go_to_pay:
        if(totalCount<=0){
          (this,"Please select the item to be paid for~",Toast.LENGTH_SHORT).show();
          return;
        }
        (this,"Money is another matter~",Toast.LENGTH_SHORT).show();
        break;
      case .tv_delete:
        if(totalCount<=0){
          (this,"Please select the product you want to delete~",Toast.LENGTH_SHORT).show();
          return;
        }
        checkDelete(());
        break;
    }
  }

  //Delete operation  private void checkDelete(Map<String,Integer> map){
    List<HashMap<String,String>> waitDeleteList=new ArrayList<>();
    Map<String,Integer> waitDeleteMap =new HashMap<>();
    for(int i=0;i<();i++){
      if(((i).get("id"))==1){
        ((i));
        ((i).get("id"),((i).get("id")));
      }
    }
    (waitDeleteList);
    (waitDeleteMap);
    priceControl(map);
    ();
  }

  //Select all or reverse  private void AllTheSelected(){
    Map<String,Integer> map=();
    boolean isCheck=false;
    boolean isUnCheck=false;
    Iterator iter = ().iterator();
    while (()) {
       entry = () ();
      if((().toString())==1)isCheck=true;
      else isUnCheck=true;
    }
    if(isCheck==true&&isUnCheck==false){//I have selected all, do reverse selection      for(int i=0;i<();i++){
        ((i).get("id"),0);
      }
      cb_check_all.setChecked(false);
    }else if(isCheck==true && isUnCheck==true){//Selection of some, make all      for(int i=0;i<();i++){
        ((i).get("id"),1);
      }
      cb_check_all.setChecked(true);
    }else if(isCheck==false && isUnCheck==true){//No one is selected, choose all      for(int i=0;i<();i++){
        ((i).get("id"),1);
      }
      cb_check_all.setChecked(true);
    }
    priceControl(map);
    (map);
    ();
  }

  private void initView(){
    listView = (ListView) findViewById();
    cb_check_all = (CheckBox) findViewById(.all_chekbox);
    tv_total_price = (TextView) findViewById(.tv_total_price);
    tv_delete = (TextView) findViewById(.tv_delete);
    tv_go_to_pay = (TextView) findViewById(.tv_go_to_pay);
    tv_go_to_pay.setOnClickListener(this);
    tv_delete.setOnClickListener(this);
    cb_check_all.setOnClickListener(this);

    adapter=new CartAdapter(this,goodsList);
    (this);
    (adapter);
    ();
  }

  private void initDate(){
    goodsList=new ArrayList<>();
    for(int i=0;i<10;i++){
      HashMap<String,String> map=new HashMap<>();
      ("id",(new Random().nextInt(10000)%(10000-2900+2900) + 2900)+"");
      ("name","The No. 1 in the Shopping Cart"+(i+1)+"Product");
      ("type",(i+20)+"code");
      ("price",(new Random().nextInt(100)%(100-29+29) + 29)+"");
      ("count",(new Random().nextInt(10)%(10-1+1) + 1)+"");
      (map);
    }

    initView();
  }
}

CartAdapter layout:

<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#CCCCCC" />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/page_backgroup"
    android:orientation="horizontal" >

    <CheckBox
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="4dp"
      android:button="@drawable/check_box_bg"
      android:checkMark="?android:attr/listChoiceIndicatorMultiple"
      android:gravity="center"
      android:minHeight="64dp"
      android:minWidth="32dp"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:visibility="visible" />

    <ImageView
      android:
      android:layout_width="85dp"
      android:layout_height="85dp"
      android:layout_marginBottom="15dp"
      android:layout_marginTop="13dp"
      android:scaleType="centerCrop"
      android:src="@mipmap/good_icon" />

    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="center_vertical"
      android:layout_marginTop="10dp"
      android:layout_marginLeft="13dp" >

      <TextView
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:ellipsize="end"
        android:maxLines="2"
        android:textColor="@color/grey_color1"
        android:textSize="14sp" />

      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:orientation="horizontal" >

        <TextView
          android:
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:singleLine="true"
          android:textColor="@color/orange_color"
          android:textSize="14sp"
          android:textStyle="bold" />

        <TextView
          android:
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:layout_marginLeft="10dp"
          android:layout_toRightOf="@+id/tv_goods_price"
          android:singleLine="true"
          android:textColor="@color/grey_color3"
          android:textSize="10sp"/>

        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:layout_marginRight="15dp"
          android:orientation="horizontal" >

          <TextView
            android:
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="@drawable/text_angle_gray"
            android:gravity="center"
            android:text="one"
            android:textColor="@color/grey_color1"
            android:textSize="12sp" />

          <TextView
            android:
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="@drawable/text_angle"
            android:gravity="center"
            android:singleLine="true"
            android:text="1"
            android:textColor="@color/grey_color1"
            android:textSize="12sp" />

          <TextView
            android:
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:background="@drawable/text_angle_right"
            android:gravity="center"
            android:text="+"
            android:textColor="@color/grey_color1"
            android:textSize="12sp" />
        </LinearLayout>
      </RelativeLayout>
    </RelativeLayout>
  </LinearLayout>

</LinearLayout>

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;

/**
 * Created by lipeng
 * 2017/6/5.
 */

public class CartAdapter extends BaseAdapter {

  private Context context;
  private List<HashMap<String,String>> dataList;
  private ViewHolder vh;
  private Map<String,Integer> pitchOnMap;
  private RefreshPriceInterface refreshPriceInterface;

  public CartAdapter(Context context, List<HashMap<String,String>> list){
    =context;
    =list;

    pitchOnMap=new HashMap<>();
    for(int i=0;i<();i++){
      ((i).get("id"),0);
    }
  }

  @Override
  public View getView(final int position, View view, ViewGroup viewGroup) {
    vh=new ViewHolder();
    if(view==null){
      view= (context).inflate(.item_layout,null);

      =(CheckBox)(.check_box);
      =(ImageView)(.iv_adapter_list_pic);
      =(TextView)(.tv_goods_name);
      =(TextView)(.tv_goods_price);
      =(TextView)(.tv_type_size);
      =(TextView)(.tv_num);
      =(TextView)(.tv_reduce);
      =(TextView)(.tv_add);

      (vh);
    }else {
      vh=(ViewHolder)();
    }

    if(()>0){

      if(((position).get("id"))==0)(false);
      else (true);
      HashMap<String,String> map=(position);
      (("name"));
      (("count"));
      (("type"));
      ("¥ "+((("price")) * (("count"))));

      (new () {
        @Override
        public void onClick(View view) {
          final int index=position;
          if(((CheckBox)view).isChecked())((index).get("id"),1);else ((index).get("id"),0);
          (pitchOnMap);
        }
      });
      (new () {
        @Override
        public void onClick(View view) {
          final int index=position;
          (index).put("count",(((index).get("count"))-1)+"");
          if(((index).get("count"))<=0){
            //You can prompt whether to delete the product. If you confirm, remove it, otherwise you will keep 1            String deID=(index).get("id");
            (index);
            (deID);
          }
          notifyDataSetChanged();
          (pitchOnMap);
        }
      });
      (new () {
        @Override
        public void onClick(View view) {
          final int index=position;
          (index).put("count",(((index).get("count"))+1)+"");
          if(((index).get("count"))>15){
            //15 is the inventory can be selected for the upper limit            (context,"The inventory limit has been reached~",Toast.LENGTH_SHORT).show();
            return;
          }
          notifyDataSetChanged();
          (pitchOnMap);
        }
      });
    }

    return view;
  }

  public Map<String,Integer> getPitchOnMap(){
    return pitchOnMap;
  }
  public void setPitchOnMap(Map<String,Integer> pitchOnMap){
    =new HashMap<>();
    (pitchOnMap);
  }

  public interface RefreshPriceInterface{
    void refreshPrice(Map<String, Integer> pitchOnMap);
  }
  public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface){
    =refreshPriceInterface;
  }

  @Override
  public Object getItem(int i) {
    return null;
  }

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

  @Override
  public int getCount() {
    if (dataList != null) {
      return ();
    } else {
      return 0;
    }
  }

  class ViewHolder{
    CheckBox checkBox;
    ImageView icon;
    TextView name,price,num,type,reduce,add;
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.