Android实现购物车功能

时间:2021-12-25 22:50:14

如图:

      Android实现购物车功能

主要代码如下:
actvity中的代码:

  publicclassShoppingCartActivity extendsBaseActivity {

  
  private List<Test> data;
  private ListView mListView;
  private ShoppingCartAdapter adapter;
  
  private RelativeLayout rlRefresh;
  private TextView tvRefresh;
  private ProgressBar barRefresh;
  private LinearLayout clear;
  private CheckBox checkBox_select_all;
  private CheckBox checkBox_add;
  private TextView integral_sum;
  private int sum = 0;
  private int[] sumIntegral;
  private Context context;
  
  @Override
  protected void onCreate(Bundle bundle) {
    // TODO Auto-generated method stub
    super.onCreate(bundle);
    setContentView(R.layout.activity_shopping_cart);
    initView();
  }
  
  private void initView() {
    context = this;
    showpage = 1;
    isPermitFlag = true;
    data = new ArrayList<Test>();
    // 测试数据
    data.add(new Test("id", "color", "type", "100"));
    data.add(new Test("id", "color", "type", "200"));
    data.add(new Test("id", "color", "type", "300"));
    data.add(new Test("id", "color", "type", "0"));
    data.add(new Test("id", "color", "type", "300"));
    data.add(new Test("id", "color", "type", "100"));
    data.add(new Test("id", "color", "type", "500"));
    data.add(new Test("id", "color", "type", "0"));
    data.add(new Test("id", "color", "type", "900"));
  
    adapter = new ShoppingCartAdapter(context, handler, data);
  
    sumIntegral = new int[data.size() + 1];
  
    checkBox_add = (CheckBox) findViewById(R.id.checkbox_add);
    integral_sum = (TextView) findViewById(R.id.integral_sum);
    clear = (LinearLayout) findViewById(R.id.clear);
    clear.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
        data.clear();
        adapter.notifyDataSetChanged();
        integral_sum.setText(0 + "");
        checkBox_select_all.setChecked(false);
        checkBox_add.setClickable(false);
      }
    });
    checkBox_select_all = (CheckBox) findViewById(R.id.checkbox_select);
    checkBox_select_all.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
            .getIsSelected();
        Iterator iterator = isSelected.entrySet().iterator();
        List<Boolean> array = new ArrayList<Boolean>();//列表中checkbox选中状态
        List<Integer> nums = new ArrayList<Integer>();//列表中商品数量
        while (iterator.hasNext()) {
          HashMap.Entry entry = (HashMap.Entry) iterator.next();
          Integer key = (Integer) entry.getKey();
          Boolean val = (Boolean) entry.getValue();
          array.add(val);
        }
        for (int i = 0; i < data.size(); i++) {
          int num = data.get(i).getNum();
          int integral = Integer.valueOf(data.get(i).getIntegral());
          nums.add(num);
        }
        if (checkBox_select_all.isChecked()) {
  
          for (int i = 0; i < data.size(); i++) {
            ShoppingCartAdapter.getIsSelected().put(i, true);
          }
          checkBox_add.setChecked(true);
          adapter.notifyDataSetChanged();
        } else {
          for (int i = 0; i < data.size(); i++) {
            ShoppingCartAdapter.getIsSelected().put(i, false);
          }
          checkBox_add.setChecked(false);
          adapter.notifyDataSetChanged();
          integral_sum.setText(0 + "");
        }
  
      }
    });
    mListView= (ListView) findViewById(R.id.finance_list);
    mListView.setAdapter(adapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {
  
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        Toast.makeText(context, position + "", Toast.LENGTH_LONG)
            .show();
  
        int pos = position - 1;
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        int num = data.get(pos).getNum();
        if (num == 0) {
          Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG)
              .show();
        } else {
          boolean cu = !ShoppingCartAdapter.getIsSelected().get(pos);
          ShoppingCartAdapter.getIsSelected().put(pos, cu);
          adapter.notifyDataSetChanged();
          //遍历获取列表中checkbox的选中状态
          HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
              .getIsSelected();
          Iterator iterator = isSelected.entrySet().iterator();
          List<Boolean> array = new ArrayList<Boolean>();
          while (iterator.hasNext()) {
            HashMap.Entry entry = (HashMap.Entry) iterator.next();
            Integer key = (Integer) entry.getKey();
            Boolean val = (Boolean) entry.getValue();
            array.add(val);
          }
          if (Test.isAllFalse(array)) {
            checkBox_select_all.setChecked(false);
            checkBox_add.setChecked(false);
          }
          if (Test.isAllTrue(array)) {
            checkBox_select_all.setChecked(true);
            checkBox_add.setChecked(true);
          }
          if (Test.isHaveOneFasle(array)) {
            checkBox_select_all.setChecked(false);
          }
          if (Test.isHaveOneTrue(array)) {
            checkBox_add.setChecked(true);
          }
        }
      }
    });
  }
  
  @SuppressLint("HandlerLeak")
  private Handler handler = new Handler(){
    @SuppressWarnings("unchecked")
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if(msg.what == 10){ //更改选中商品的总价格
        float price = (Float)msg.obj;
        if(price > 0){
          integral_sum.setText(price+"");
        }else{
          integral_sum.setText("0");
        }
      }
      else if(msg.what == 11){
        //列表选中状态
        List<Boolean> array = (List<Boolean>) msg.obj;
        if (Test.isAllFalse(array)) {
          checkBox_select_all.setChecked(false);
          checkBox_add.setChecked(false);
        }
        if (.isAllTrue(array)) {
          checkBox_select_all.setChecked(true);
          checkBox_add.setChecked(true);
        }
        if (Test.isHaveOneFasle(array)) {
          checkBox_select_all.setChecked(false);
        }
        if (Test.isHaveOneTrue(array)) {
          checkBox_add.setChecked(true);
        }
      }
    }
  };
 

actvity中XML的代码:

  <?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:header=http://schemas.android.com/apk/res/com.sxc.test"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/app_background"
  android:orientation="vertical" >
  
  <com.autoserve.core.widget.HeaderWidget
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    header:text="我的购物车" />
  
  <LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@id/header"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="@color/white"
    android:orientation="horizontal" >
  
    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_marginLeft="10dp"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:orientation="horizontal" >
  
      <CheckBox
        android:id="@+id/checkbox_select"
        style="@style/CustomCheckboxTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="全选"
        android:textColor="@color/gry_666666"
        android:textSize="@dimen/small_size" />
    </LinearLayout>
  
    <LinearLayout
      android:id="@+id/clear"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_marginRight="20dp"
      android:gravity="center_vertical|right"
      android:orientation="horizontal" >
  
      <CheckBox
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:background="@drawable/clear" />
  
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="清空购物车"
        android:textColor="#b61d1d"
        android:textSize="@dimen/small_size" />
    </LinearLayout>
  </LinearLayout>
  
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/layout1"
    android:layout_marginBottom="50dp"
    android:orientation="vertical" >
  
    <View
      android:layout_width="match_parent"
      android:layout_height="0.1dp"
      android:background="@color/divider_color" />
  
    <ListView
      android:id="@+id/finance_list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="false"
      android:divider="@color/lucency" />
  
    <include
      layout="@layout/include_refresh"
      android:visibility="gone" />
  </LinearLayout>
  
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >
  
    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2"
      android:background="@color/gry_999999"
      android:gravity="center_vertical"
      android:orientation="horizontal" >
  
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:orientation="horizontal" >
  
        <CheckBox
          android:id="@+id/checkbox_add"
          style="@style/CustomCheckboxTheme2"
          android:layout_width="wrap_content"
          android:clickable="false"
          android:layout_height="wrap_content" />
      </LinearLayout>
  
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >
  
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="合计:"
          android:textColor="@color/white"
          android:textSize="@dimen/small_size" />
  
        <TextView
          android:id="@+id/integral_sum"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:text="0"
          android:textColor="@color/theme_color"
          android:textSize="@dimen/small_size" />
  
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="积分"
          android:textColor="@color/white"
          android:textSize="@dimen/small_size" />
      </LinearLayout>
    </LinearLayout>
  
    <TextView
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@color/theme_color"
      android:gravity="center"
      android:text="结算"
      android:textColor="@color/white"
      android:textSize="@dimen/small_size" />
  </LinearLayout>
  
</RelativeLayout>

XML中头部可以到网上找一个这里就不放上来了
checkbox和button的样式可以根据个人喜好设置

Adaper中的代码:

  publicclassShoppingCartAdapter extendsBaseAdapter {

  
  private Context context;
  private List<Test> loans;
  private LayoutInflater inflater;
  private static HashMap<Integer, Boolean> isSelected;
  private static HashMap<Integer, Integer> numbers;
  private Handler handler;
  int num;// 商品数量
  
  static class ViewHolder { // 自定义控件集合
    public CheckBox ck_select;
    public ImageView pic_goods;
    public TextView id_goods;
    public TextView color_goods;
    public TextView type_goods;
    public TextView integral_goods;
    public AddMinusWidget add_minus;
    public LinearLayout layout;
    public TextView number;
    public Button minus;
    public Button plus;
  }
  
  /**
   * 实例化Adapter
   
   * @param context
   * @param data
   */
  public ShoppingCartAdapter(Context context, Handler handler, List<Test> data) {
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    this.loans = data;
    this.handler = handler;
    isSelected = new HashMap<Integer, Boolean>();
    numbers = new HashMap<Integer, Integer>();
    initDate();
  }
  
  private void initDate() {
    for (int i = 0; i < loans.size(); i++) {
      getIsSelected().put(i, false);
      getNumbers().put(i, 1);
    }
  }
  
  @Override
  public int getCount() {
    return loans.size();
  }
  
  @Override
  public Object getItem(int position) {
    return loans.get(position);
  }
  
  @Override
  public long getItemId(int position) {
    return position;
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // 自定义视图
    ViewHolder itemView = null;
    if (convertView == null) {
      // 获取list_item布局文件的视图
      itemView = new ViewHolder();
      convertView = inflater.inflate(R.layout.list_shopping_cart_item,
          null);
      // 获取控件对象
      itemView.ck_select = (CheckBox) convertView
          .findViewById(R.id.ck_select);
      itemView.pic_goods = (ImageView) convertView
          .findViewById(R.id.pic_goods);
      itemView.id_goods = (TextView) convertView
          .findViewById(R.id.id_goods);
      itemView.color_goods = (TextView) convertView
          .findViewById(R.id.color_goods);
      itemView.type_goods = (TextView) convertView
          .findViewById(R.id.type_goods);
      itemView.integral_goods = (TextView) convertView
          .findViewById(R.id.integral_goods);
      itemView.number = (TextView) convertView.findViewById(R.id.number);
      itemView.minus = (Button) convertView.findViewById(R.id.minus);
      itemView.plus = (Button) convertView.findViewById(R.id.plus);
      convertView.setTag(itemView);
    } else {
      itemView = (ViewHolder) convertView.getTag();
    }
  
    init(itemView, position);
  
    itemView.ck_select.setChecked(getIsSelected().get(position));
    itemView.number.setText(getNumbers().get(position).toString());
    if (getIsSelected().get(position)) {
      itemView.ck_select.setChecked(true);
    } else {
      itemView.ck_select.setChecked(false);
    }
  
    String a = itemView.number.getText().toString();
    loans.get(position).setNum(Integer.valueOf(a));
  
    Test test = loans.get(position);
    itemView.id_goods.setText((CharSequence) test.getId());
    itemView.color_goods.setText((CharSequence) test.getColor());
    itemView.type_goods.setText((CharSequence) test.getType());
    itemView.integral_goods.setText((CharSequence) test.getIntegral());
    itemView.pic_goods.setImageResource(R.drawable.shopping);
    return convertView;
  }
  
  private void init(final ViewHolder itemView, final int position) {
  
    itemView.ck_select
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
          @Override
          public void onCheckedChanged(CompoundButton buttonView,
              boolean isChecked) {
            isSelected.put(position, true);
            getIsSelected().put(position, isChecked);
            itemView.ck_select.setChecked(getIsSelected().get(
                position));
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
  
            Iterator iterator = isSelected.entrySet().iterator();
            List<Boolean> array = new ArrayList<Boolean>();
            while (iterator.hasNext()) {
              HashMap.Entry entry = (HashMap.Entry) iterator
                  .next();
              Integer key = (Integer) entry.getKey();
              Boolean val = (Boolean) entry.getValue();
              array.add(val);
            }
            handler.sendMessage(handler.obtainMessage(11, array));
          }
        });
  
    final String numString = itemView.number.getText().toString();
    itemView.plus.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        if (numString == null || numString.equals("")) {
          num = 1;
          itemView.number.setText("1");
        } else {
          if (++num < 1) // 先加,再判断
          {
            num--;
  
            Toast.makeText(context, "请输入一个大于0的数字",
                Toast.LENGTH_SHORT).show();
          } else {
            itemView.number.setText(String.valueOf(num));
            loans.get(position).setNum(num);
            numbers.put(position, num);
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
            Log.i("test", "+:" + num);
          }
        }
      }
    });
    itemView.minus.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        if (numString == null || numString.equals("")) {
          num = 1;
          itemView.number.setText("1");
        } else {
          if (--num < 1) // 先加,再判断
          {
            num++;
            Log.i("test", "-:" + num);
            Toast.makeText(context, "请输入一个大于0的数字",
                Toast.LENGTH_SHORT).show();
            Log.i("test", "-:" + num);
          } else {
            itemView.number.setText(String.valueOf(num));
            Log.i("test", "-:" + num);
            loans.get(position).setNum(num);
            numbers.put(position, num);
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
          }
        }
  
      }
    });
  
  }
  
  /**
   * 计算选中商品的积分
   
   * @return 返回需要付费的总积分
   */
  private float getTotalPrice() {
    Test bean = null;
    float totalPrice = 0;
    for (int i = 0; i < loans.size(); i++) {
      bean = loans.get(i);
      if (ShoppingCartAdapter.getIsSelected().get(i)) {
        totalPrice += bean.getNum()
            * Integer.valueOf(bean.getIntegral());
      }
    }
    return totalPrice;
  }
  
  public static HashMap<Integer, Boolean> getIsSelected() {
    return isSelected;
  }
  
  public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
    ShoppingCartAdapter.isSelected = isSelected;
  }
  
  public static HashMap<Integer, Integer> getNumbers() {
    return numbers;
  }
  
  public static void setNumbers(HashMap<Integer, Integer> numbers) {
    ShoppingCartAdapter.numbers = numbers;
  }
}
 
 
Adapter中的XML代码:
  <?xmlversion="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:descendantFocusability="blocksDescendants"
  android:background="@color/white"
  
  android:orientation="vertical" >
  <View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/divider_color" />
  
  <LinearLayout
    android:id="@+id/layout5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >
  
    <CheckBox
      android:id="@+id/ck_select"
      style="@style/CustomCheckboxTheme"
      android:layout_width="wrap_content"
      android:focusable="false"
      android:layout_height="wrap_content"
      android:layout_marginRight="5dp" />
  
    <ImageView
      android:id="@+id/pic_goods"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:scaleType="fitCenter"
      android:src="@drawable/shopping" />
  
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:orientation="vertical" >
  
      <TextView
        android:id="@+id/id_goods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短袜男士星期 POLO棉袜潮男秋冬款礼盒装"
        android:textColor="@color/gry_999999"
        android:textSize="@dimen/small_size" />
  
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
  
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" >
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="颜色:"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/color_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="黑色"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
          </LinearLayout>
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="规格:"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/type_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="普通"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
          </LinearLayout>
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="所需积分"
              android:textColor="@color/theme_color"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/integral_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="1000"
              android:layout_marginLeft="5dp"
              android:textColor="@color/theme_color"
              android:textSize="12sp" />
          </LinearLayout>
        </LinearLayout>
  
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="25dp"
          android:layout_gravity="bottom"
          android:layout_marginBottom="5dp"
          android:layout_marginRight="5dp"
          android:gravity="right"
          android:orientation="horizontal" >
  
          <LinearLayout
    android:layout_width="80dp"
    android:layout_height="25dp"
    android:layout_gravity="right"
    android:background="@color/white"
    android:orientation="horizontal" >
  
    <Button
      android:id="@+id/minus"
      android:layout_width="25dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:focusable="false"
      android:text="-"
      android:textColor="@color/black" >
    </Button>
  
    <TextView
      android:id="@+id/number"
      android:layout_width="30dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:inputType="number"
      android:text="1"
      android:textColor="@color/black" >
    </TextView>
  
    <Button
      android:id="@+id/plus"
      android:layout_width="25dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:focusable="false"
      android:text="+"
      android:textColor="@color/black" >
    </Button>
  </LinearLayout>
        </LinearLayout>
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
  
  <View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/divider_color" />
  
</LinearLayout>
 
实体类:
  publicclassTest {
  @Override
  public String toString() {
    return "test [id=" + id + ", color=" + color
        + ", type=" + type + ", integral=" + integral + "]";
  }
  
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getColor() {
    return color;
  }
  public void setColor(String color) {
    this.color = color;
  }
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  public String getIntegral() {
    return integral;
  }
  public void setIntegral(String integral) {
    this.integral = integral;
  }
  private String id;
  private String color;
  private String type;
  private String integral;
  private int num;//商品数量
  private int sumIntegral;
  private boolean isChoosed;   //商品是否在购物车中被选中
  
  public Test(String id, String color, String type, String integral) {
    super();
    this.id = id;
    this.color = color;
    this.type = type;
    this.integral = integral;
  }
  public Test() {
    super();
  }
  
  public int getNum() {
    return num;
  }
  
  public void setNum(int num) {
    this.num = num;
  }
  
  public int getSumIntegral() {
    return sumIntegral;
  }
  
  public void setSumIntegral(int sumIntegral) {
    this.sumIntegral = sumIntegral;
  }
  
  public boolean isChoosed() {
    return isChoosed;
  }
  
  public void setChoosed(boolean isChoosed) {
    this.isChoosed = isChoosed;
  }  
}

  

主要代码如下:
actvity中的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
public class ShoppingCartActivity extends BaseActivity {
  
  private List<Test> data;
  private ListView mListView;
  private ShoppingCartAdapter adapter;
  
  private RelativeLayout rlRefresh;
  private TextView tvRefresh;
  private ProgressBar barRefresh;
  private LinearLayout clear;
  private CheckBox checkBox_select_all;
  private CheckBox checkBox_add;
  private TextView integral_sum;
  private int sum = 0;
  private int[] sumIntegral;
  private Context context;
  
  @Override
  protected void onCreate(Bundle bundle) {
    // TODO Auto-generated method stub
    super.onCreate(bundle);
    setContentView(R.layout.activity_shopping_cart);
    initView();
  }
  
  private void initView() {
    context = this;
    showpage = 1;
    isPermitFlag = true;
    data = new ArrayList<Test>();
    // 测试数据
    data.add(new Test("id", "color", "type", "100"));
    data.add(new Test("id", "color", "type", "200"));
    data.add(new Test("id", "color", "type", "300"));
    data.add(new Test("id", "color", "type", "0"));
    data.add(new Test("id", "color", "type", "300"));
    data.add(new Test("id", "color", "type", "100"));
    data.add(new Test("id", "color", "type", "500"));
    data.add(new Test("id", "color", "type", "0"));
    data.add(new Test("id", "color", "type", "900"));
  
    adapter = new ShoppingCartAdapter(context, handler, data);
  
    sumIntegral = new int[data.size() + 1];
  
    checkBox_add = (CheckBox) findViewById(R.id.checkbox_add);
    integral_sum = (TextView) findViewById(R.id.integral_sum);
    clear = (LinearLayout) findViewById(R.id.clear);
    clear.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
        data.clear();
        adapter.notifyDataSetChanged();
        integral_sum.setText(0 + "");
        checkBox_select_all.setChecked(false);
        checkBox_add.setClickable(false);
      }
    });
    checkBox_select_all = (CheckBox) findViewById(R.id.checkbox_select);
    checkBox_select_all.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
            .getIsSelected();
        Iterator iterator = isSelected.entrySet().iterator();
        List<Boolean> array = new ArrayList<Boolean>();//列表中checkbox选中状态
        List<Integer> nums = new ArrayList<Integer>();//列表中商品数量
        while (iterator.hasNext()) {
          HashMap.Entry entry = (HashMap.Entry) iterator.next();
          Integer key = (Integer) entry.getKey();
          Boolean val = (Boolean) entry.getValue();
          array.add(val);
        }
        for (int i = 0; i < data.size(); i++) {
          int num = data.get(i).getNum();
          int integral = Integer.valueOf(data.get(i).getIntegral());
          nums.add(num);
        }
        if (checkBox_select_all.isChecked()) {
  
          for (int i = 0; i < data.size(); i++) {
            ShoppingCartAdapter.getIsSelected().put(i, true);
          }
          checkBox_add.setChecked(true);
          adapter.notifyDataSetChanged();
        } else {
          for (int i = 0; i < data.size(); i++) {
            ShoppingCartAdapter.getIsSelected().put(i, false);
          }
          checkBox_add.setChecked(false);
          adapter.notifyDataSetChanged();
          integral_sum.setText(0 + "");
        }
  
      }
    });
    mListView= (ListView) findViewById(R.id.finance_list);
    mListView.setAdapter(adapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {
  
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        Toast.makeText(context, position + "", Toast.LENGTH_LONG)
            .show();
  
        int pos = position - 1;
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        int num = data.get(pos).getNum();
        if (num == 0) {
          Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG)
              .show();
        } else {
          boolean cu = !ShoppingCartAdapter.getIsSelected().get(pos);
          ShoppingCartAdapter.getIsSelected().put(pos, cu);
          adapter.notifyDataSetChanged();
          //遍历获取列表中checkbox的选中状态
          HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
              .getIsSelected();
          Iterator iterator = isSelected.entrySet().iterator();
          List<Boolean> array = new ArrayList<Boolean>();
          while (iterator.hasNext()) {
            HashMap.Entry entry = (HashMap.Entry) iterator.next();
            Integer key = (Integer) entry.getKey();
            Boolean val = (Boolean) entry.getValue();
            array.add(val);
          }
          if (Test.isAllFalse(array)) {
            checkBox_select_all.setChecked(false);
            checkBox_add.setChecked(false);
          }
          if (Test.isAllTrue(array)) {
            checkBox_select_all.setChecked(true);
            checkBox_add.setChecked(true);
          }
          if (Test.isHaveOneFasle(array)) {
            checkBox_select_all.setChecked(false);
          }
          if (Test.isHaveOneTrue(array)) {
            checkBox_add.setChecked(true);
          }
        }
      }
    });
  }
  
  @SuppressLint("HandlerLeak")
  private Handler handler = new Handler(){
    @SuppressWarnings("unchecked")
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if(msg.what == 10){ //更改选中商品的总价格
        float price = (Float)msg.obj;
        if(price > 0){
          integral_sum.setText(price+"");
        }else{
          integral_sum.setText("0");
        }
      }
      else if(msg.what == 11){
        //列表选中状态
        List<Boolean> array = (List<Boolean>) msg.obj;
        if (Test.isAllFalse(array)) {
          checkBox_select_all.setChecked(false);
          checkBox_add.setChecked(false);
        }
        if (.isAllTrue(array)) {
          checkBox_select_all.setChecked(true);
          checkBox_add.setChecked(true);
        }
        if (Test.isHaveOneFasle(array)) {
          checkBox_select_all.setChecked(false);
        }
        if (Test.isHaveOneTrue(array)) {
          checkBox_add.setChecked(true);
        }
      }
    }
  };

actvity中XML的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:header=http://schemas.android.com/apk/res/com.sxc.test"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/app_background"
  android:orientation="vertical" >
  
  <com.autoserve.core.widget.HeaderWidget
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    header:text="我的购物车" />
  
  <LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@id/header"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="@color/white"
    android:orientation="horizontal" >
  
    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_marginLeft="10dp"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:orientation="horizontal" >
  
      <CheckBox
        android:id="@+id/checkbox_select"
        style="@style/CustomCheckboxTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="全选"
        android:textColor="@color/gry_666666"
        android:textSize="@dimen/small_size" />
    </LinearLayout>
  
    <LinearLayout
      android:id="@+id/clear"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_marginRight="20dp"
      android:gravity="center_vertical|right"
      android:orientation="horizontal" >
  
      <CheckBox
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:background="@drawable/clear" />
  
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="清空购物车"
        android:textColor="#b61d1d"
        android:textSize="@dimen/small_size" />
    </LinearLayout>
  </LinearLayout>
  
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/layout1"
    android:layout_marginBottom="50dp"
    android:orientation="vertical" >
  
    <View
      android:layout_width="match_parent"
      android:layout_height="0.1dp"
      android:background="@color/divider_color" />
  
    <ListView
      android:id="@+id/finance_list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="false"
      android:divider="@color/lucency" />
  
    <include
      layout="@layout/include_refresh"
      android:visibility="gone" />
  </LinearLayout>
  
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >
  
    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="2"
      android:background="@color/gry_999999"
      android:gravity="center_vertical"
      android:orientation="horizontal" >
  
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:orientation="horizontal" >
  
        <CheckBox
          android:id="@+id/checkbox_add"
          style="@style/CustomCheckboxTheme2"
          android:layout_width="wrap_content"
          android:clickable="false"
          android:layout_height="wrap_content" />
      </LinearLayout>
  
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >
  
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="合计:"
          android:textColor="@color/white"
          android:textSize="@dimen/small_size" />
  
        <TextView
          android:id="@+id/integral_sum"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:text="0"
          android:textColor="@color/theme_color"
          android:textSize="@dimen/small_size" />
  
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="积分"
          android:textColor="@color/white"
          android:textSize="@dimen/small_size" />
      </LinearLayout>
    </LinearLayout>
  
    <TextView
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@color/theme_color"
      android:gravity="center"
      android:text="结算"
      android:textColor="@color/white"
      android:textSize="@dimen/small_size" />
  </LinearLayout>
  
</RelativeLayout>

-XML中头部可以到网上找一个这里就不放上来了
.checkbox和button的样式可以根据个人喜好设置。

Adaper中的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
public class ShoppingCartAdapter extends BaseAdapter {
  
  private Context context;
  private List<Test> loans;
  private LayoutInflater inflater;
  private static HashMap<Integer, Boolean> isSelected;
  private static HashMap<Integer, Integer> numbers;
  private Handler handler;
  int num;// 商品数量
  
  static class ViewHolder { // 自定义控件集合
    public CheckBox ck_select;
    public ImageView pic_goods;
    public TextView id_goods;
    public TextView color_goods;
    public TextView type_goods;
    public TextView integral_goods;
    public AddMinusWidget add_minus;
    public LinearLayout layout;
    public TextView number;
    public Button minus;
    public Button plus;
  }
  
  /**
   * 实例化Adapter
   
   * @param context
   * @param data
   */
  public ShoppingCartAdapter(Context context, Handler handler, List<Test> data) {
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    this.loans = data;
    this.handler = handler;
    isSelected = new HashMap<Integer, Boolean>();
    numbers = new HashMap<Integer, Integer>();
    initDate();
  }
  
  private void initDate() {
    for (int i = 0; i < loans.size(); i++) {
      getIsSelected().put(i, false);
      getNumbers().put(i, 1);
    }
  }
  
  @Override
  public int getCount() {
    return loans.size();
  }
  
  @Override
  public Object getItem(int position) {
    return loans.get(position);
  }
  
  @Override
  public long getItemId(int position) {
    return position;
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // 自定义视图
    ViewHolder itemView = null;
    if (convertView == null) {
      // 获取list_item布局文件的视图
      itemView = new ViewHolder();
      convertView = inflater.inflate(R.layout.list_shopping_cart_item,
          null);
      // 获取控件对象
      itemView.ck_select = (CheckBox) convertView
          .findViewById(R.id.ck_select);
      itemView.pic_goods = (ImageView) convertView
          .findViewById(R.id.pic_goods);
      itemView.id_goods = (TextView) convertView
          .findViewById(R.id.id_goods);
      itemView.color_goods = (TextView) convertView
          .findViewById(R.id.color_goods);
      itemView.type_goods = (TextView) convertView
          .findViewById(R.id.type_goods);
      itemView.integral_goods = (TextView) convertView
          .findViewById(R.id.integral_goods);
      itemView.number = (TextView) convertView.findViewById(R.id.number);
      itemView.minus = (Button) convertView.findViewById(R.id.minus);
      itemView.plus = (Button) convertView.findViewById(R.id.plus);
      convertView.setTag(itemView);
    } else {
      itemView = (ViewHolder) convertView.getTag();
    }
  
    init(itemView, position);
  
    itemView.ck_select.setChecked(getIsSelected().get(position));
    itemView.number.setText(getNumbers().get(position).toString());
    if (getIsSelected().get(position)) {
      itemView.ck_select.setChecked(true);
    } else {
      itemView.ck_select.setChecked(false);
    }
  
    String a = itemView.number.getText().toString();
    loans.get(position).setNum(Integer.valueOf(a));
  
    Test test = loans.get(position);
    itemView.id_goods.setText((CharSequence) test.getId());
    itemView.color_goods.setText((CharSequence) test.getColor());
    itemView.type_goods.setText((CharSequence) test.getType());
    itemView.integral_goods.setText((CharSequence) test.getIntegral());
    itemView.pic_goods.setImageResource(R.drawable.shopping);
    return convertView;
  }
  
  private void init(final ViewHolder itemView, final int position) {
  
    itemView.ck_select
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
          @Override
          public void onCheckedChanged(CompoundButton buttonView,
              boolean isChecked) {
            isSelected.put(position, true);
            getIsSelected().put(position, isChecked);
            itemView.ck_select.setChecked(getIsSelected().get(
                position));
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
  
            Iterator iterator = isSelected.entrySet().iterator();
            List<Boolean> array = new ArrayList<Boolean>();
            while (iterator.hasNext()) {
              HashMap.Entry entry = (HashMap.Entry) iterator
                  .next();
              Integer key = (Integer) entry.getKey();
              Boolean val = (Boolean) entry.getValue();
              array.add(val);
            }
            handler.sendMessage(handler.obtainMessage(11, array));
          }
        });
  
    final String numString = itemView.number.getText().toString();
    itemView.plus.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        if (numString == null || numString.equals("")) {
          num = 1;
          itemView.number.setText("1");
        } else {
          if (++num < 1) // 先加,再判断
          {
            num--;
  
            Toast.makeText(context, "请输入一个大于0的数字",
                Toast.LENGTH_SHORT).show();
          } else {
            itemView.number.setText(String.valueOf(num));
            loans.get(position).setNum(num);
            numbers.put(position, num);
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
            Log.i("test", "+:" + num);
          }
        }
      }
    });
    itemView.minus.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
        if (numString == null || numString.equals("")) {
          num = 1;
          itemView.number.setText("1");
        } else {
          if (--num < 1) // 先加,再判断
          {
            num++;
            Log.i("test", "-:" + num);
            Toast.makeText(context, "请输入一个大于0的数字",
                Toast.LENGTH_SHORT).show();
            Log.i("test", "-:" + num);
          } else {
            itemView.number.setText(String.valueOf(num));
            Log.i("test", "-:" + num);
            loans.get(position).setNum(num);
            numbers.put(position, num);
            handler.sendMessage(handler.obtainMessage(10,
                getTotalPrice()));
          }
        }
  
      }
    });
  
  }
  
  /**
   * 计算选中商品的积分
   
   * @return 返回需要付费的总积分
   */
  private float getTotalPrice() {
    Test bean = null;
    float totalPrice = 0;
    for (int i = 0; i < loans.size(); i++) {
      bean = loans.get(i);
      if (ShoppingCartAdapter.getIsSelected().get(i)) {
        totalPrice += bean.getNum()
            * Integer.valueOf(bean.getIntegral());
      }
    }
    return totalPrice;
  }
  
  public static HashMap<Integer, Boolean> getIsSelected() {
    return isSelected;
  }
  
  public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
    ShoppingCartAdapter.isSelected = isSelected;
  }
  
  public static HashMap<Integer, Integer> getNumbers() {
    return numbers;
  }
  
  public static void setNumbers(HashMap<Integer, Integer> numbers) {
    ShoppingCartAdapter.numbers = numbers;
  }
}

Adapter中的XML代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?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:descendantFocusability="blocksDescendants"
  android:background="@color/white"
  
  android:orientation="vertical" >
  <View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/divider_color" />
  
  <LinearLayout
    android:id="@+id/layout5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >
  
    <CheckBox
      android:id="@+id/ck_select"
      style="@style/CustomCheckboxTheme"
      android:layout_width="wrap_content"
      android:focusable="false"
      android:layout_height="wrap_content"
      android:layout_marginRight="5dp" />
  
    <ImageView
      android:id="@+id/pic_goods"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:scaleType="fitCenter"
      android:src="@drawable/shopping" />
  
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:orientation="vertical" >
  
      <TextView
        android:id="@+id/id_goods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短袜男士星期 POLO棉袜潮男秋冬款礼盒装"
        android:textColor="@color/gry_999999"
        android:textSize="@dimen/small_size" />
  
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
  
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" >
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="颜色:"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/color_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="黑色"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
          </LinearLayout>
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="规格:"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/type_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="普通"
              android:textColor="@color/gry_999999"
              android:textSize="12sp" />
          </LinearLayout>
  
          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
  
            <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="所需积分"
              android:textColor="@color/theme_color"
              android:textSize="12sp" />
  
            <TextView
              android:id="@+id/integral_goods"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="1000"
              android:layout_marginLeft="5dp"
              android:textColor="@color/theme_color"
              android:textSize="12sp" />
          </LinearLayout>
        </LinearLayout>
  
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="25dp"
          android:layout_gravity="bottom"
          android:layout_marginBottom="5dp"
          android:layout_marginRight="5dp"
          android:gravity="right"
          android:orientation="horizontal" >
  
          <LinearLayout
    android:layout_width="80dp"
    android:layout_height="25dp"
    android:layout_gravity="right"
    android:background="@color/white"
    android:orientation="horizontal" >
  
    <Button
      android:id="@+id/minus"
      android:layout_width="25dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:focusable="false"
      android:text="-"
      android:textColor="@color/black" >
    </Button>
  
    <TextView
      android:id="@+id/number"
      android:layout_width="30dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:inputType="number"
      android:text="1"
      android:textColor="@color/black" >
    </TextView>
  
    <Button
      android:id="@+id/plus"
      android:layout_width="25dp"
      android:layout_height="match_parent"
      android:background="@drawable/kuangzi1"
      android:gravity="center"
      android:focusable="false"
      android:text="+"
      android:textColor="@color/black" >
    </Button>
  </LinearLayout>
        </LinearLayout>
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
  
  <View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/divider_color" />
  
</LinearLayout>

实体类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.autoserve.net33.model;
  
  
public class Test {
  @Override
  public String toString() {
    return "test [id=" + id + ", color=" + color
        + ", type=" + type + ", integral=" + integral + "]";
  }
  
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getColor() {
    return color;
  }
  public void setColor(String color) {
    this.color = color;
  }
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  public String getIntegral() {
    return integral;
  }
  public void setIntegral(String integral) {
    this.integral = integral;
  }
  private String id;
  private String color;
  private String type;
  private String integral;
  private int num;//商品数量
  private int sumIntegral;
  private boolean isChoosed;   //商品是否在购物车中被选中
  
  public Test(String id, String color, String type, String integral) {
    super();
    this.id = id;
    this.color = color;
    this.type = type;
    this.integral = integral;
  }
  public Test() {
    super();
  }
  
  public int getNum() {
    return num;
  }
  
  public void setNum(int num) {
    this.num = num;
  }
  
  public int getSumIntegral() {
    return sumIntegral;
  }
  
  public void setSumIntegral(int sumIntegral) {
    this.sumIntegral = sumIntegral;
  }
  
  public boolean isChoosed() {
    return isChoosed;
  }
  
  public void setChoosed(boolean isChoosed) {
    this.isChoosed = isChoosed;
  }  
}

以上就是本文的全部内容,祝大家在新的一年里工作顺利,事事顺心,我们大家共同努力。

Android实现购物车功能的更多相关文章

  1. Android 购物车功能的实现

    首先,众所周知,ListView是Android最常用的控件,可以说是最简单的控件,也可以说是最复杂的控件. 作为一个Android初级开发者,可能会简单的ListView展示图文信息. 作为一个有一 ...

  2. Android Studio调试功能使用总结【转】

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  3. 【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能

    Cookie是服务器存放在客户端的一些数据,比如密码,以及你曾经访问过的一些数据. 设置Cookie //设置cookie Cookie cookie = new Cookie("TOM&q ...

  4. 给destoon商城的列表中和首页添加购物车功能

    如何给destoon商城的列表中和首页添加购物车功能? 目前加入购物车的功能只存在商城的详细页面里,有时候我们需要批量购买的时候,希望在列表页就能够使用这个加入购物车的功能. 修改步骤见下: 例如在商 ...

  5. Android 实现闹钟功能

      原文地址:Android 实现闹钟功能作者:Android_Learners 一.手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以 ...

  6. ASP&period;NET之电子商务系统开发-2&lpar;购物车功能&rpar;

    一.前言 继上次的首页数据列表后,这是第二篇.记录一下购物车这个比较庞大的功能,可能实现的方法跟其他人有点不一样,不过原理都差不多,是将cookie存数据库里面的. 二.开始 首先看一下购物车流程及对 ...

  7. jQuery 复制节点的元素实现加入到购物车功能

    描写叙述: 用户点击左边div中的商品,相应商品会自己主动加入到右面的div中,类似电子商城中的加入到购物车功能. 主要用到了jquery中的复制节点功能,基本原理是首先获取点击的元素,然后将对应信息 ...

  8. Android Studio调试功能使用总结---转

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  9. 微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

随机推荐

  1. Python-06-面向对象(基础篇)

    面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机 ...

  2. C&num;&colon;时间转换

    1.C#时间转js时间 /// <summary> /// C#时间转js时间 /// </summary> /// <param name="theDate& ...

  3. stm32f系列单片机startup&lowbar;stm32fxxx&period;s文件说明(转)

    * 文件名          : startup_stm32f10x_hd.s;* 库版本           : V3.5.0;* 说明:             此文件为STM32F10x高密度设 ...

  4. poll实现

    struct pollfd {    int fd;     //当前描述符    short events;     //进程关心的该描述符的事件    short revents;    //返回 ...

  5. BZOJ2542&colon; &lbrack;Ctsc2001&rsqb;终极情报网

    题解: 乘积最小只需要取对数.然后反向边就变成1/c,而不是-c了. 精度问题搞得我已经我想说什么了... 贴一份网上的pascal 代码: type ss=record x,y,c,r,next:l ...

  6. 【洛谷】SAC E&num;1 Factorial

    别人可以眼杀我却研究了一个小时看了题解才懂的数学题 输入: n, k 输出: n!在k进制下后缀0的个数 n,k <= 10^12 将 n! 表示成 x×2y5z 的形式,其中 x mod 2 ...

  7. Map&period;putAll&lpar;&rpar;用法

    import Java.util.HashMap; public class Map_putAllTest {public static void main(String[] args){   //两 ...

  8. class threading&period;Thread&lpar;&rpar;说明:

    class threading.Thread()说明: class threading.Thread(group=None, target=None, name=None, args=(), kwar ...

  9. netty12---线程池简单源码

    package com.cn; import java.io.IOException; import java.nio.channels.Selector; import java.util.Queu ...

  10. ICO流程,casestudy

    https://medium.com/crypto-oracle/ico-analysis-framework-nex-case-study-bf65586b4b32