本文实例为大家分享了Android Studio实现简单购物车的具体代码,供大家参考,具体内容如下

MainActivity的布局文件

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">

android:id="@+id/top_bar"

android:layout_width="match_parent"

android:layout_height="48dp"

android:background="#E24146"

android:orientation="vertical" >

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" />

android:id="@+id/listview"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:childIndicator="@null"

android:groupIndicator="@null" >

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="2.5"

android:gravity="center_vertical"

android:orientation="horizontal" >

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" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="5dp"

android:text="合计:"

android:textSize="16sp"

android:textStyle="bold" />

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" />

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" />

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" />

条目的布局文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#CCCCCC" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

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" />

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"

/>

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="center_vertical"

android:layout_marginTop="10dp"

android:layout_marginLeft="13dp" >

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" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_marginBottom="30dp"

android:orientation="horizontal" >

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="价格"/>

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"/>

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" >

android:id="@+id/tv_reduce"

android:layout_width="25dp"

android:layout_height="25dp"

android:gravity="center"

android:background="#ccc"

android:text="一"

android:textSize="12sp" />

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" />

android:id="@+id/tv_add"

android:layout_width="25dp"

android:layout_height="25dp"

android:gravity="center"

android:text="十"

android:background="#ccc"

android:textSize="12sp" />

CartAdapter适配器

public class CaetAdapter extends BaseAdapter {

private Context context;

private List> list;

private HashMap pitchOnMap;

public HashMap getPitchOnMap() {

return pitchOnMap;

}

public void setPitchOnMap(HashMap pitchOnMap) {

this.pitchOnMap = pitchOnMap;

}

public CaetAdapter(Context context, List> 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;

}

/**

* 创建接口

*/

public interface RefreshPriceInterface {

/**

* 把价格展示到总价上

* @param pitchOnMap

*/

void refreshPrice(HashMap pitchOnMap);

}

/**

* 定义一个接口对象

*/

private RefreshPriceInterface mrefreshPriceInterface;

/**

* 向外部暴露一个方法

* 把价格展示到总价上

* @param refreshPriceInterface

*/

public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) {

mrefreshPriceInterface = refreshPriceInterface;

}

}

MainActivity

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 goodsList;

private UserDao userDao;

private List> 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() {

//创建集合

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

HashMap 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 pitchOnMap) {

priceControl(pitchOnMap);

}

/**

* 控制价格展示总价

*/

private void priceControl(Map pitchOnMap){

totalCount = 0;

totalPrice = 0.00;

for(int i=0;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 map){

List> waitDeleteList=new ArrayList<>();

Map waitDeleteMap =new HashMap<>();

for(int i=0;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 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

map.put(listmap.get(i).get("id"),0);

}

all_chekbox.setChecked(false);

}else if(isCheck==true && isUnCheck==true){//部分选择,做全选

for(int i=0;i

map.put(listmap.get(i).get("id"),1);

}

all_chekbox.setChecked(true);

}else if(isCheck==false && isUnCheck==true){//一个没选,做全选

for(int i=0;i

map.put(listmap.get(i).get("id"),1);

}

all_chekbox.setChecked(true);

}

priceControl(map);

adapter.setPitchOnMap(map);

adapter.notifyDataSetChanged();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android长按加入购物车,Android Studio实现简单购物车功能相关推荐

  1. android 购物车实现,Android Studio实现简单购物车功能

    Android Studio实现简单购物车功能 发布时间:2020-08-30 17:23:56 来源:脚本之家 阅读:241 作者:攀岩嘉 本文实例为大家分享了Android九宫格图片展示的具体代码 ...

  2. android长截屏代码,android长截屏原理及实现代码

    android长截屏原理及实现代码 发布时间:2020-08-31 06:55:16 来源:脚本之家 阅读:158 作者:Android笔记 小米系统自带的长截屏应该很多人都用过,效果不错.当长截屏时 ...

  3. android 环信 emmessagelistener,集成环信实现简单聊天功能

    废话不多说,先上效果图 下面来实现 1.第一步导入jar or so 去环信下载sdk导入到我们的项目中 导入完成之后我们区项目的build文件中,android括号的最下方加上以下代码 不然jar无 ...

  4. 美多商城之购物车(展示商品页面简单购物车)

    三.展示商品页面简单购物车 需求:用户鼠标悬停在商品页面右上角购物车标签上,以下拉框形式展示当前购物车数据. 3.1. 简单购物车数据接口设计和定义 1.请求方式 选项 方案 请求方法 GET 请求地 ...

  5. android长截图工具下载,Android实现长截屏功能

    本文实例为大家分享了Android实现长截屏功能的具体代码,供大家参考,具体内容如下 1.MainActivity public class MainActivity extends AppCompa ...

  6. android长按动画效果,android 实现按住说话功能

    今天工作上需要做一个仿微信语音聊天中的按住说话的功能.其实很简单,主要就是利用MediaRecorder实现录音,用MediaPlayer实现播放功能.下面我就具体说一下是怎么实现的. 1,首先对按钮 ...

  7. 阿语python美多商城-商品-购物车管理之第7.3节展示商品页面简单购物车

    展示商品页面简单购物车 需求:用户鼠标悬停在商品页面右上角购物车标签上,以下拉框形式展示当前购物车数据. 1. 简单购物车数据接口设计和定义 1.请求方式 选项 方案 请求方法 GET 请求地址 /c ...

  8. Android Studio开发——蓝牙聊天功能

    Android Studio开发--蓝牙聊天功能 蓝牙工作流程 功能要求 实现要点 声明蓝牙权限 添加程序运行的状态描述文本及配色代码 布局文件 蓝牙会话的服务组件ChatService Activi ...

  9. Android Studio实现简单的健身系统,一起锻炼身体吧

    文章目录 一.系统背景 二.系统概述 三.开发环境 四.系统结构 五.详细设计 5.1.RecycleView 5.2.ViewPager 5.3.OkHttp 六.运行演示 七.系统总结 八.源码获 ...

最新文章

  1. python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
  2. JavaScript: 取得 function 的所有参数名
  3. log4j简介及应用
  4. magento去除子分类的url地址中带有父分类的url key
  5. 文件过滤_jmeter(七)-BeanShell对数据过滤保存文件
  6. 某中学要对学校运动会进行计算机管理,2020年新编高职入学考试适应性试卷信息技术试卷定稿名师精品资料....
  7. poj 1330(LCA)
  8. ctfshow中Misc入门WP(超级全)
  9. 【数学】求一类数列的通项公式
  10. 给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false
  11. mysql中查询一个商品价格最大的商品名称的sql出错记录
  12. 2022年最新陕西水利水电施工安全员考试题库及答案
  13. window用户切换
  14. HTML中将图片进行放大缩小
  15. Qt父窗体子窗体获取绝对相对坐标全面剖析(待续)
  16. 【组合数学】指数生成函数 ( 指数生成函数概念 | 排列数指数生成函数 = 组合数普通生成函数 | 指数生成函数示例 )
  17. 基础商务谈判培训技巧
  18. H5页面新增二维码、条形码扫描识别功能
  19. C#使用西门子S7 协议读写PLC DB块
  20. Python实现美国费城Danny`s Wok中餐馆菜单分析

热门文章

  1. 【路径规划】基于matlab DWA算法机器人局部避障路径规划【含Matlab源码 890期】
  2. IPv4 客户与IPv6服务器_UNP
  3. 10 Kafka开荒
  4. 商务网站建设与维护【17】
  5. Android之底部弹框
  6. Linux虚拟机联网设置详细教程
  7. 0欧姆电阻能流过无穷大电流吗
  8. 保存的流程图怎么找不到_别找了,找不到了!最赞的流程图工具都在这!
  9. 【Java 输入、输出流】
  10. Linux RTC驱动模型分析之rtc-sysfs.c【转】