Android仿淘宝购物车demo

2023-05-16

       夏的热情渐渐退去,秋如期而至,丰收的季节,小编继续着实习之路,走着走着,就走到了购物车,逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件,对于爱购物的姑娘来说,购物车里面的商品恐怕是爆满,添加不进去了,以前逛淘宝的时候,小编没有想过要怎么样实现购物车,就知道在哪儿一个劲儿的逛,但是现在不一样了,小编做为一个开发者,想的就是该如何实现,捣鼓了两天的时间,用listview来实现,已经有模有样了,现在小编就来简单的总结一下实现购物车的心路历程,帮助有需要的小伙伴,欢迎小伙伴们留言交流。

       首先,小编简单的介绍一下listview,ListView 控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。 可使用 ListView 控件将称作 ListItem 对象的列表条目组织成下列四种不同的视图之一:1.大(标准)图标2.小图标3.列表4.报表 View 属性决定在列表中控件使用何种视图显示项目。还可用 LabelWrap 属性控制列表中与项目关联的标签是否可换行显示。另外,还可管理列表中项目的排序方法和选定项目的外观。今天小编主要和小伙伴们分享一下,如何使用listview实现购物的功能。做过Android的小伙伴都知道一个xml对应一个java类,但是购物车有点不一样,因为她里面的商品有可能不只一件,所以我们需要有两个xml,两个java类,相对应的还需要一个适配器adapter,一个model,下面小编来详细的介绍一下实现购物车的过程。

        第一步,写model层,类似我们之前写过的实体层,具体代码如下所示:

        

/***
 * 说明:购物车的相关信息
 * 作者:丁国华
 * 时间:2015年8月10日 09:41:18
 */
package jczb.shoping.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.R.string;

public class shoppingCart implements Serializable {
	
	    private String proImg;
	    private String ProName; 
	    private String shopPrice; 
	    private String markPrice; 
	    private String proCount;
		public String getProImg() {
			return proImg;
		}
		public void setProImg(String proImg) {
			this.proImg = proImg;
		}
		public String getProName() {
			return ProName;
		}
		public void setProName(String proName) {
			ProName = proName;
		}
		public String getShopPrice() {
			return shopPrice;
		}
		public void setShopPrice(String shopPrice) {
			this.shopPrice = shopPrice;
		}
		public String getMarkPrice() {
			return markPrice;
		}
		public void setMarkPrice(String markPrice) {
			this.markPrice = markPrice;
		}
		public String getProCount() {
			return proCount;
		}
		public void setProCount(String proCount) {
			this.proCount = proCount;
		}
	    
	  }
        第二步,我们编写xml里面的文件,需要编写两个xml文件,首先我们来编写activity_shoppingcart.xml的文件,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout 
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:background="#438FCB"
          android:orientation="horizontal">
          
          <!-- 尖括号的布局 -->
          <ImageView 
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:padding="8dp"
               android:src="@drawable/tb_icon_actionbar_back" />
         <!-- 购物车的布局 -->
         <TextView
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="5.49"
             android:gravity="center"
             android:text="购物车"
             android:textColor="#FFFFFF"
             android:textSize="20sp"/>
         <!-- 编辑的布局 -->
         <TextView 
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="3.18"
             android:gravity="center"
             android:text="编辑"
             android:textColor="#FFFFFF"
             android:textSize="20sp" />
    </LinearLayout>
    <!-- listview,购物车里面的东西有可能比较多,需要用listview来进行显示 -->
    <LinearLayout
	            android:layout_width="fill_parent"
	            android:layout_height="wrap_content"
	            android:layout_weight="1" 
	            android:orientation="vertical"
	            android:layout_marginTop="0dp">
         
        <ListView 
		     android:id="@+id/cart_shopping_listview"
		     android:layout_width="wrap_content"
		     android:layout_height="wrap_content"
		     android:divider="#808080"
		     android:dividerHeight="0.5dp">
    	</ListView>
         
    </LinearLayout>
 
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_alignParentBottom="true"
       android:orientation="horizontal">
         <!-- 全选的布局 -->
         <CheckBox 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="10dp"
             android:text="全选"/>
         <!-- 合计的布局 -->
         <TextView
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:gravity="right"
             android:paddingRight="10dp"
             android:textColor="#F63A19"
             android:text="合计:¥88"/>
         <!-- 去结算的布局 -->
         
        <TextView
             android:id="@+id/jiesuan_button"
             android:layout_width="80dp"
             android:layout_height="wrap_content"
             android:layout_marginRight="10dp"
             android:background="@drawable/android_login_color"
             android:gravity="center"
             android:padding="10dp"
             android:text="结算"/>
   </LinearLayout>
    
</LinearLayout >
       我们来看一下xml布局的效果,如下图所示:

        

       接着我们来布局第二个xml,activity_shoppingcart_item.xml,代码如下所示:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
         <!-- 小对勾的布局 -->

       <CheckBox
           android:id="@+id/pro_checkbox"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:focusable="false"
           android:focusableInTouchMode="false" />

         <!-- 图片布局 -->
         <ImageView 
             android:id="@+id/pro_image"
             android:layout_width="80dp" 
             android:layout_height="80dp"
             android:layout_margin="5dp"
             android:scaleType="centerCrop"
             android:src="@drawable/detail_show_1"/>
         <!-- 商品名称和价格的布局 -->
         <LinearLayout 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content"
             android:orientation="vertical">
             <!-- 商品名称的布局 -->
             <TextView 
                 android:id="@+id/pro_name"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="10dp"
                 android:text="连衣裙女夏季"
                 />
             <!-- 价格的布局 -->

             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="33dp"
                 android:orientation="horizontal" >
			      
		<TextView
			 android:id="@+id/pro_shopPrice"
	                 android:layout_width="wrap_content"
	                 android:layout_height="wrap_content"
	                 android:layout_gravity="bottom"
	                 android:layout_marginTop="10dp"
	                 android:text="¥88"
	                 android:textSize="16sp"/>
			      
			<!-- <TextView
			 android:id="@+id/pro_markPrice"
	                 android:layout_width="wrap_content"
	                 android:layout_height="wrap_content"
	                 android:layout_gravity="bottom"
	                 android:layout_marginTop="10dp"
	                 android:text="¥66"
	                 android:textSize="16sp"/> -->
                </LinearLayout>
             
             <LinearLayout
                 android:layout_width="150dp"
                 android:layout_height="33dp"
                 android:orientation="horizontal" >
			       <!-- 加号 -->
                  <Button
                      android:id="@+id/pro_add"
                      android:layout_width="wrap_content"
                      android:layout_height="34dp"
                      android:text="+" />
                  
                  <TextView
                     android:id="@+id/pro_count"
	                 android:layout_width="wrap_content"
	                 android:layout_height="wrap_content"
	                 android:layout_gravity="bottom"
	                 android:layout_marginTop="10dp"
	                 android:text="88"
	                 android:textSize="13sp"/>
                  

			         <!-- 减号-->
                    <Button
                      android:id="@+id/pro_reduce"
                      android:layout_width="wrap_content"
                      android:layout_height="34dp"
                      android:layout_marginRight="0dp"
                      android:text="-" />
                 
                 </LinearLayout>
          </LinearLayout>
 </LinearLayout>

</LinearLayout>
       布局效果如下所示:

         

       第三步、我们来编写适配器adapter中的代码,即ShoppingCartAdapter,具体代码如下所示:

package jczb.shoping.adapter;

import java.util.List;

import cn.jpush.android.data.r;

import jczb.shoping.adapter.productsListAdapter.ViewHolder;
import jczb.shoping.adapter.productsListAdapter.searchList;
import jczb.shoping.model.productSonSorting_cate;
import jczb.shoping.model.shoppingCart;
import jczb.shoping.model.sonSortigns;
import jczb.shoping.ui.R;
import jczb.shoping.ui.ShoppingCartActivity;
import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShoppingCartAdapter extends BaseAdapter {

	private Context mContext; 
	private List<shoppingCart> mList;
	
	public ShoppingCartAdapter(Context mContext,List<shoppingCart> mList) { 
		super(); 
		this.mContext = mContext; 
		this.mList = mList; 
		}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mList==null) {
			return 0;
		}else {
			return this.mList.size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (mList == null) { 
			return null; 
		} else { 
			return this.mList.get(position); 
		} 
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null; 
		if (convertView == null) { 
			holder = new ViewHolder(); 
	convertView = LayoutInflater.from(this.mContext).inflate(R.layout.activity_shoppingcart_item, null,true);
			holder.image=(ImageView) convertView.findViewById(R.id.pro_image);
			holder.chose=(CheckBox) convertView.findViewById(R.id.pro_checkbox);
			holder.proName=(TextView) convertView.findViewById(R.id.pro_name);
			holder.proPrice=(TextView)convertView.findViewById(R.id.pro_shopPrice);
			holder.proCount=(TextView) convertView.findViewById(R.id.pro_count);
			convertView.setTag(holder); 
			
		} else { 
			holder = (ViewHolder) convertView.getTag(); 
		} 
		
		if (this.mList != null) { 
		    shoppingCart shoppingList=this.mList.get(position);
		    holder.proName.setText(shoppingList.getProName().toString());
		    holder.proPrice.setText(shoppingList.getShopPrice().toString());
		    holder.proCount.setText(shoppingList.getProCount().toString());
		   
		} 
		return convertView; 
	}

	/*定义item对象*/
	public class ViewHolder {
		ImageView image;
		TextView proName;
		CheckBox chose;
		TextView proPrice;
		TextView proCount;
		
  }
	
}
        第四步,编写java类里面的代码,我们先来编写ShoppingCartItemActivity.java中的内容,具体代码如下所示:

package jczb.shoping.ui;
import android.app.Activity;
import android.os.Bundle;

public class ShoppingCartItemActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoppingcart_item);
        
  }
}

        第五步,编写ShoppingCartActivity.java里面的内容,如下所示:

package jczb.shoping.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jczb.shoping.adapter.ShoppingCartAdapter;
import jczb.shoping.common.AgentApi;
import jczb.shoping.model.shoppingCart;
import jczb.shoping.ui.SearchActivity.ViewHolder;
import jczb.shoping.ui.ShoppingcartActivity2.myThread;

import com.alibaba.fastjson.JSON;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShoppingCartActivity extends Activity{
	
	TextView jiesuan,proName,shopPrice,proCount;
	ListView aListView;
	
	private LayoutInflater layoutInflater;
	
	private TextView name;
	
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoppingcart);
        
        findViewByID();
        
        /*开始线程*/
       new Thread(new myThread()).start();{

	}
	
	
	
	 /*根据ID找到控件*/
	  public void findViewByID(){
          aListView=(ListView) findViewById(R.id.cart_shopping_listview);
		}
	//开辟线程
		
		public class myThread implements Runnable {
			public void run() {			
				Message msg = new Message();						
				
				
				try {
					Map<String, String> parmas = new HashMap<String, String>();
					parmas.put("username", "1");
					parmas.put("password", "2");
		String url = "http://192.168.1.110:8080/SchoolShopJson/ShoppingCart.txt";
					
					// 要发送的数据和访问的地址					
					String result = AgentApi.dopost(parmas, url);														
				
 // 如果返回的为空或者初始化时输入的ip地址无效(会返回下面的字符串),说明服务器连接失败!
					if (result == null) {
						// 使用-1代表服务器连接失败
						msg.what = -1;
					} else {
	                    msg.what=1;
	                    msg.obj=result;  
					}
				} catch (Exception e) {
					e.printStackTrace();						
					// 使用-1代表程序异常
					msg.what = -2;
					msg.obj = e;
				}
				mHandler.sendMessage(msg);	
				
			}
		}
		
		protected void  initView() {
			// TODO Auto-generated method stub
		}
				
				
		 /*子线程-解析数据*/
		 private  Handler mHandler = new Handler(){
	    	public void handleMessage(Message msg) {
				switch (msg.what) {
				case -1:
					Toast.makeText(ShoppingCartActivity.this, "服务器连接失败!",
							Toast.LENGTH_SHORT).show();
					break;
				case -2:
					Toast.makeText(ShoppingCartActivity.this, "哎呀,出错啦...",
							Toast.LENGTH_SHORT).show();
					break;				
				case 1:				
					String temp = (String)msg.obj;							
						

					//将拿到的json转换为数组
	       List<shoppingCart> ShoppingcartInfo = JSON.parseArray(temp,shoppingCart.class);
					
		ListView.setAdapter(new ShoppingCartAdapter(ShoppingCartActivity.this, ShoppingcartInfo));
                    break;
					
				default:
					break;
				}
			}
		};
	    
}

       我们来看一下运行的效果,如下所示:

        

      小编寄语:该博文,小编主要简单的介绍了如何实现购物车,使用listview显示多件商品,总的实现思路就是先写model,接着写xml里面,写完xml写adapter适配器里面的内容,最后写java里面的代码。购物车实现了,但是小编到现在还是云里雾里,不过没关系,小编会越挫越勇的,这就是生命的意义,还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段实习的日子里,安卓带给小编的种种的惊喜。


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android仿淘宝购物车demo 的相关文章

随机推荐

  • 华为、TCL、大疆面试经历!32K高薪996和18K朝九晚五,我该怎么选?

    背景 楼主双非吊车尾一本毕业 xff0c 14年正式进入编程行业 xff0c 从事Android移动开发 一些个人的原因 xff0c 年前从一家公司裸辞 xff0c 带着老婆孩子离开了杭州 本想着回家过完年也就差不多赶上 金三银四 黄金季
  • PX4源码分析6_uorb通信机制

    一 创建流程 xff1a 在Firmware msg下创建msg文件 xff0c eg xff1a xxx msg xff0c 内容格式仿照原有msg文件在Firmware msg CMakeLists txt中将对应的msg文件添加 xf
  • PX4源码分析7_添加mavlink自定义消息

    一 自定义mavlink消息 xff1a 根据uorb消息 xff08 msg xff09 自定义mavlink消息 方法为利用mavlink generator工具在xml文件生成mavlink所需相应的头文件 二 发送自定义mavlin
  • Unix网络编程中第一个例子出现connect error: Connection refused

    这个问题我来回答 xff0c linux 现在因为安全问题 xff0c 各个发行版本默认是不开daytime服务的 由于要打开端口 xff0c 所以服务端程序需要用root权限执行 xff0c 所以你可以先 sudo daytimetcps
  • linux内存分配方法

    基于linux 驱动开发以及应用开发 当想要分配内存的时候 要面对很多的内存分配函数 malloc kmalloc vmalloc alloc page dma alloc 简单归纳如下 malloc 分配用户空间内存 不保证物理连续 xf
  • 算术移动和逻辑移动

    算术左移 逻辑左移 算术右移 逻辑右移有什么不同 算术左移 xff1a 末尾添0 xff1b 逻辑左移 xff1a 末尾添0 xff1b 算术右移 xff1a 左端最低位填充 xff1b 逻辑右移 xff1a 左端添0 算术左移和算术右移主
  • 素数的验证范围到数的开方

    假设范围大于数的开方 xff0c 则另个数必定小于数的开方 若两个数都大于数的开方 xff0c 则相乘必定大于这个数 所以只需要验证2到这个数的开方范围内就可以
  • CPU时间

    CPU时间 xff08 进程时间 xff09 xff1a 用户CPU时间 43 系统CPU时间 用户CPU时间 xff1a 用户态进程执行时间 系统CPU时间 xff1a 内核态进程执行时间 时钟时间 xff08 墙上时钟时间 xff09
  • 硬中断、软中断和信号

    硬中断是外部设备对CPU的中断 xff0c 软中断是中断底半部的一种处理机制 xff0c 信号则是由内核 xff08 或其他进程 xff09 对某个进程的中断 硬中断是由外部事件引起的因此具有随机性和突发性 xff1b 软中断是执行中断指令
  • CMakeLists.txt----一点儿自己的经验教训

    被CMakeLists txt坑过数次 xff08 大多是被自己蠢哭 xff09 xff0c 把用过的命令稍微记一下 注意 xff1a 中文空格等什么诡异字符 xff0c 链接库的名字不要忘记 xff0c 不要写错 好记性不如烂键盘 xff
  • docker命令之push

    1 背景 NAME docker push Push an image or a repository to the registry SYNOPSIS docker push NAME TAG DESCRIPTION Push an im
  • C标准库源码解剖(4):字符串处理函数string.h和wchar.h

    string h中包含了所有的字符串处理函数 xff0c 也包含了内存处理函数 xff0c 因为这些内存处理函数 xff08 如比如 复制 搜索 xff09 的功能与字符串处理函数功能类似 我们是用通用指针来指向内存块的 xff0c 通用指
  • VS 2019 + Clang-Format

    VS 2019配置Clang Format Clang Format选项 span class token comment 语言 None Cpp Java JavaScript ObjC Proto TableGen TextProto
  • C语言bmp图片读取写入

    一 介绍 bmp是我们常用的一种图片格式 xff0c 这里用c语言写一个程序打开 xff0c 读取像素点然后增亮 增亮公式 y 61 kx 43 b 这里用的24位的真彩bmp图片 二 实现 img bmp c span class tok
  • 树莓派设置wifi自动连接

    1 sd卡烧录好镜像 2 在boot目录创建 wpa supplicant conf 内容 span class token assign left variable ctrl interface span span class token
  • 电路城 电路方案 51单片机开发板(原理图+PCB+学习程序源文件)

    1 89C52单片机 xff0c 支持USB口或者是串口两种下载程序方式 xff1b 2 6位数码管 xff08 做动态扫描及静态显示实验 xff09 3 8位LED发光二极管 xff08 做流水灯实验 xff09 4 MAX232芯片RS
  • 信息系统开发与管理

    信息化是这个时代的主旋律 xff0c 如何执她之手 xff0c 跟上她的节拍 xff0c 不掉队 xff0c 我相信 xff0c 聪明的读者 xff0c 你的答案一定跃然于心底 一本 信息系统开发与管理 xff0c 结合学生信息管理系统 x
  • 在与SQL Server建立连接时出现与网络相关的或特定于实例的错误

    向往前一样 xff0c 学习牛腩新闻发布系统的视频 xff0c 敲代码 xff0c 打开数据库 xff0c 出现一个框框 xff0c 详细内容如下 xff1a 数据库连接不上 xff0c 所有的工作都要歇班 xff0c 捣鼓了会儿 xff0
  • Sql Server服务远程过程调用失败

    由于开发系统 xff0c 需要vs版本统一 xff0c 于是经过了昨天一整天艰苦卓绝的斗争 xff0c 小编终于成功的写在了13版本的vs xff0c 重新装上了12版本的vs xff0c 本来想着 xff0c 12版本的vs搭建成功了 x
  • Android仿淘宝购物车demo

    夏的热情渐渐退去 xff0c 秋如期而至 xff0c 丰收的季节 xff0c 小编继续着实习之路 xff0c 走着走着 xff0c 就走到了购物车 xff0c 逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件 xff0c 对于爱购