一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android Studio實現簡單購物車功能

Android Studio實現簡單購物車功能

2022-03-10 15:31攀巖嘉 Android

這篇文章主要為大家詳細介紹了Android Studio實現簡單購物車,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android Studio實現簡單購物車的具體代碼,供大家參考,具體內容如下

MainActivity的布局文件

?
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity">
 
  <LinearLayout
    android:id="@+id/top_bar"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#E24146"
    android:orientation="vertical" >
    <TextView
      android:id="@+id/title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:minHeight="48dp"
      android:text="購物車"
      android:textColor="#ffffff"
      android:textSize="17sp" />
  </LinearLayout>
 
  <ListView
    android:id="@+id/listview"
    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:id="@+id/all_chekbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="4dp"
        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="合計:"
        android:textSize="16sp"
        android:textStyle="bold" />
 
      <TextView
        android:id="@+id/tv_total_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="¥0.00"
        android:textSize="16sp"
        android:textStyle="bold" />
    </LinearLayout>
 
    <TextView
      android:id="@+id/tv_delete"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:clickable="true"
      android:background="#a29e9e"
      android:gravity="center"
      android:text="刪除"
      android:textColor="#FAFAFA" />
 
    <TextView
      android:id="@+id/tv_go_to_pay"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="#E24146"
      android:clickable="true"
      android:gravity="center"
      android:text="付款(0)"
      android:textColor="#FAFAFA" />
  </LinearLayout>
</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
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  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:orientation="horizontal" >
 
    <CheckBox
      android:id="@+id/check_box"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="4dp"
      android:checkMark="?android:attr/listChoiceIndicatorMultiple"
      android:gravity="center"
      android:minHeight="64dp"
      android:minWidth="32dp"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:visibility="visible" />
 
    <ImageView
      android:id="@+id/iv_adapter_list_pic"
      android:layout_width="85dp"
      android:layout_height="85dp"
      android:layout_marginBottom="15dp"
      android:layout_marginTop="13dp"
      android:scaleType="centerCrop"
      android:src="@mipmap/ic_launcher"
       />
 
    <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:id="@+id/tv_goods_name"
        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:text="商品"
        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:id="@+id/tv_goods_price"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:singleLine="true"
          android:textSize="14sp"
          android:textStyle="bold"
          android:text="價格"/>
 
        <TextView
          android:id="@+id/tv_type_size"
          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: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:id="@+id/tv_reduce"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:gravity="center"
            android:background="#ccc"
            android:text="一"
            android:textSize="12sp" />
 
          <TextView
            android:id="@+id/tv_num"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:gravity="center"
            android:singleLine="true"
            android:text="1"
            android:textSize="12sp" />
 
          <TextView
            android:id="@+id/tv_add"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:gravity="center"
            android:text="十"
            android:background="#ccc"
            android:textSize="12sp" />
        </LinearLayout>
      </RelativeLayout>
    </RelativeLayout>
  </LinearLayout>
 
</LinearLayout>

CartAdapter適配器

?
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
public class CaetAdapter extends BaseAdapter {
 
  private Context context;
  private List<HashMap<String, String>> list;
  private HashMap<String, Integer> pitchOnMap;
 
  public HashMap<String, Integer> getPitchOnMap() {
    return pitchOnMap;
  }
 
  public void setPitchOnMap(HashMap<String, Integer> pitchOnMap) {
    this.pitchOnMap = pitchOnMap;
  }
 
  public CaetAdapter(Context context, List<HashMap<String, String>> list) {
    this.context = context;
    this.list = list;
 
    pitchOnMap = new HashMap<>();
    for (int i = 0; i < list.size(); i++) {
      pitchOnMap.put(list.get(i).get("id"), 0);
    }
  }
 
  @Override
  public int getCount() {
    return list.size();
  }
 
  @Override
  public Object getItem(int position) {
    return list.get(position);
  }
 
  @Override
  public long getItemId(int position) {
    return position;
  }
 
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    convertView = View.inflate(context, R.layout.item_layout, null);
    final CheckBox checkBox;
    ImageView icon;
    final TextView name, price, num, type, reduce, add;
 
    checkBox = convertView.findViewById(R.id.check_box);
    icon = convertView.findViewById(R.id.iv_adapter_list_pic);
    name = convertView.findViewById(R.id.tv_goods_name);
    price = convertView.findViewById(R.id.tv_goods_price);
    type = convertView.findViewById(R.id.tv_type_size);
    num = convertView.findViewById(R.id.tv_num);
    reduce = convertView.findViewById(R.id.tv_reduce);
    add = convertView.findViewById(R.id.tv_add);
 
    name.setText(list.get(position).get("name"));
    price.setText("¥ " + (Integer.valueOf(list.get(position).get("price"))) * (Integer.valueOf(list.get(position).get("count"))));
    type.setText(list.get(position).get("type"));
    num.setText(list.get(position).get("count"));
 
    if(pitchOnMap.get(list.get(position).get("id"))== 0){
      checkBox.setChecked(false);
    }else{
      checkBox.setChecked(true);
    }
 
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(checkBox.isChecked()){
          pitchOnMap.put(list.get(position).get("id"),1);
        }else{
          pitchOnMap.put(list.get(position).get("id"), 0);
        }
        mrefreshPriceInterface.refreshPrice(pitchOnMap);
      }
    });
 
    //商品數量減
    reduce.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (Integer.valueOf(list.get(position).get("count")) <= 1) {
          Toast.makeText(context, "數量不能再減啦,只能刪除!", Toast.LENGTH_SHORT).show();
        } else {
          list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) - 1) + "");
          notifyDataSetChanged();
        }
        mrefreshPriceInterface.refreshPrice(pitchOnMap);
      }
    });
    //商品數量加
    add.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) + 1) + "");
        notifyDataSetChanged();
        mrefreshPriceInterface.refreshPrice(pitchOnMap);
 
      }
 
    });
 
    return convertView;
  }
 
  /**
   * 創(chuàng)建接口
   */
  public interface RefreshPriceInterface {
    /**
     * 把價格展示到總價上
     * @param pitchOnMap
     */
    void refreshPrice(HashMap<String, Integer> pitchOnMap);
  }
 
  /**
   * 定義一個接口對象
   */
  private RefreshPriceInterface mrefreshPriceInterface;
 
  /**
   * 向外部暴露一個方法
   * 把價格展示到總價上
   * @param refreshPriceInterface
   */
  public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) {
    mrefreshPriceInterface = refreshPriceInterface;
  }
 
 
}

MainActivity

?
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
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CaetAdapter.RefreshPriceInterface{
 
  private LinearLayout top_bar;
  private ListView listview;
  private CheckBox all_chekbox;
  private TextView price;
  private TextView delete;
  private TextView tv_go_to_pay;
 
  private List<User> goodsList;
  private UserDao userDao;
  private List<HashMap<String,String>> listmap=new ArrayList<>();
  private CaetAdapter adapter;
 
   private double totalPrice = 0.00;
  private int totalCount = 0;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }
 
  private void initView() {
    top_bar = (LinearLayout) findViewById(R.id.top_bar);
    listview = (ListView) findViewById(R.id.listview);
    all_chekbox = (CheckBox) findViewById(R.id.all_chekbox);
    price = (TextView) findViewById(R.id.tv_total_price);
    delete = (TextView) findViewById(R.id.tv_delete);
    tv_go_to_pay = (TextView) findViewById(R.id.tv_go_to_pay);
 
    all_chekbox.setOnClickListener(this);
    delete.setOnClickListener(this);
    tv_go_to_pay.setOnClickListener(this);
 
    initDate();
    adapter = new CaetAdapter(MainActivity.this, listmap);
    listview.setAdapter(adapter);
    adapter.setRefreshPriceInterface(this);
  }
 
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.all_chekbox:
        AllTheSelected();
        break;
      case R.id.tv_delete:
        checkDelete(adapter.getPitchOnMap());
        break;
      case R.id.tv_go_to_pay:
        if(totalCount<=0){
          Toast.makeText(this,"請選擇要付款的商品~",Toast.LENGTH_SHORT).show();
        }else{
          Toast.makeText(this,"付款成功",Toast.LENGTH_SHORT).show();
        }
        break;
    }
  }
  /**
   * 數據
   */
  private void initDate() {
    //創(chuàng)建集合
    goodsList = new ArrayList<>();
    //數據庫
    userDao = Myapplication.getInstances().getDaoSession().getUserDao();
    userDao.deleteAll();
    //數據源
    for (int i = 0; i < 10; i++) {
      //向數據庫存放數據
      User user = new User((long) i,
          "購物車里的第" + (i + 1) + "件商品",
          (i + 20) + "碼",
          "10",
          "10");
      userDao.insert(user);
    }
    //從數據庫中把數據放到集合中
    goodsList=userDao.loadAll();
    //把結合中的數據放到HashMap集合中
    for(int i=0;i<goodsList.size();i++){
      HashMap<String,String> map=new HashMap<>();
      map.put("id",goodsList.get(i).getId()+"");
      map.put("name",goodsList.get(i).getName());
      map.put("type",(goodsList.get(i).getType()));
      map.put("price",goodsList.get(i).getPrice()+"");
      map.put("count",goodsList.get(i).getCount()+"");
      listmap.add(map);
    }
  }
 
  @Override
  public void refreshPrice(HashMap<String, Integer> pitchOnMap) {
    priceControl(pitchOnMap);
  }
 
  /**
   * 控制價格展示總價
   */
  private void priceControl(Map<String, Integer> pitchOnMap){
    totalCount = 0;
    totalPrice = 0.00;
    for(int i=0;i<listmap.size();i++){
      if(pitchOnMap.get(listmap.get(i).get("id"))==1){
        totalCount=totalCount+Integer.valueOf(listmap.get(i).get("count"));
        double goodsPrice=Integer.valueOf(listmap.get(i).get("count"))*Double.valueOf(listmap.get(i).get("price"));
        totalPrice=totalPrice+goodsPrice;
      }
    }
    price.setText(" ¥ "+totalPrice);
    tv_go_to_pay.setText("付款("+totalCount+")");
  }
 
  /**
   * 刪除 控制價格展示總價
   * @param map
   */
  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<listmap.size();i++){
      if(map.get(listmap.get(i).get("id"))==1){
        waitDeleteList.add(listmap.get(i));
        waitDeleteMap.put(listmap.get(i).get("id"),map.get(listmap.get(i).get("id")));
      }
  }
    listmap.removeAll(waitDeleteList);
    map.remove(waitDeleteMap);
    priceControl(map);
    adapter.notifyDataSetChanged();
  }
  /**
   *全選或反選
   */
  private void AllTheSelected(){
    HashMap<String,Integer> map=adapter.getPitchOnMap();
    boolean isCheck=false;
    boolean isUnCheck=false;
    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
 
      if(Integer.valueOf(entry.getValue().toString())==1){
        isCheck=true;
      }else{
        isUnCheck=true;
      }
    }
    if(isCheck==true&&isUnCheck==false){//已經全選,做反選
      for(int i=0;i<listmap.size();i++){
        map.put(listmap.get(i).get("id"),0);
      }
      all_chekbox.setChecked(false);
    }else if(isCheck==true && isUnCheck==true){//部分選擇,做全選
      for(int i=0;i<listmap.size();i++){
        map.put(listmap.get(i).get("id"),1);
      }
      all_chekbox.setChecked(true);
    }else if(isCheck==false && isUnCheck==true){//一個沒選,做全選
      for(int i=0;i<listmap.size();i++){
        map.put(listmap.get(i).get("id"),1);
      }
      all_chekbox.setChecked(true);
    }
    priceControl(map);
    adapter.setPitchOnMap(map);
    adapter.notifyDataSetChanged();
  }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qpdb19981023/article/details/80848088

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品国产一区二区第一页 | 性做久久久久久久 | 鬼吹灯天星术在线高清观看 | 国产在线观看人成激情视频 | 亚洲第一免费播放区 | 亚洲国产欧美在线成人aaaa | 无码国产成人午夜在线观看不卡 | 女人把私密部位张开让男人桶 | 亚洲第一免费播放区 | 国产91短视频 | 国产一卡二卡3卡4卡四卡在线 | 白丝美女用胸伺候主人 | 亚洲色图欧美偷拍 | 亚洲人成网站在线观看青青 | 欧美日韩一区二区三区免费 | 色综合久久中文字幕网 | 深夜视频免费看 | 美女被狂干 | 国产色视频一区二区三区 | 国产亚洲玖玖玖在线观看 | 教室里老师好紧h | 亚洲一区二区三区91 | 亚洲精品免费在线 | 逼逼爱| 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 | 9热在线精品视频观看 | 91小视频在线观看免费版高清 | 色呦呦网 | 国产馆| 国产精品福利久久2020 | 麻豆最新地址 | 三级午夜宅宅伦不卡在线 | 亚洲欧美日韩国产一区二区精品 | 日韩欧美国产免费看清风阁 | 大乳奶水bbw | 亚洲日本中文字幕在线2022 | 欧美亚洲激情在线 | 日韩欧美一区二区在线观看 | 精品一区heyzo在线播放 | 国产欧美日韩高清专区ho | 男人的天堂久久精品激情 |