Android 仿京东购物车

来源:互联网 发布:js防水材料能外墙吗 编辑:程序博客网 时间:2024/05/22 07:56

因为项目中需要用到购物车,在网上找了很久,一直没有找到一个满意的购物车demo,所以自己花了一天时间,仿写了一个,因为项目中的购物车没有接口,所以购物车的商品信息都是从商品详情哪里得来的,具体步骤如下:

一、获取商品信息添加到sqlite:

这一步我就不详细说了,怎么添加信息到sqlite中,不是很懂的同学可以去我之前写的Android SQLite增删查改哪里查看,有详细代码实现。

点击打开链接

二、购物车页面以及item如下:

listview:

<?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" >   <RelativeLayout        android:id="@+id/rlt_detail_produt_title"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:background="@drawable/shangdaohanglan" >        <TextView            android:id="@+id/tv_detail_product_title"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginLeft="60dp"            android:layout_marginRight="60dp"            android:ellipsize="marquee"            android:focusable="true"            android:focusableInTouchMode="true"            android:gravity="center"            android:marqueeRepeatLimit="marquee_forever"            android:singleLine="true"            android:text="购物车"            android:textColor="@color/text_title_color"            android:textSize="20sp" />        <TextView            android:id="@+id/shopping_detail_product_back"            android:layout_width="50dp"            android:layout_height="50dp"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"            android:drawableLeft="@drawable/fanhui"            android:paddingLeft="20dp" />        <TextView            android:id="@+id/shopping_detele_table"            android:layout_width="wrap_content"            android:layout_height="20dp"            android:layout_alignParentBottom="true"            android:layout_alignParentRight="true"            android:layout_margin="5dp"            android:gravity="center"            android:paddingLeft="20dp"            android:text="清空购物车"            android:textColor="@color/text_title_color"            android:textSize="15sp" />    </RelativeLayout>   <LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="vertical" >       <ListView           android:id="@+id/shopping_order_listview1"           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:visibility="visible"                       >       </ListView>   </LinearLayout>   <include layout="@layout/shoppin_button" />   </LinearLayout>
item

<?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="horizontal" >    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:orientation="vertical"            android:gravity="center_vertical"             >            <CheckBox                android:id="@+id/shopping_checkBox1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="true"                 />        </LinearLayout>        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginLeft="5dp"            android:orientation="vertical" >            <ImageView                android:id="@+id/shopping_images"                android:layout_width="80dp"                android:layout_height="80dp"                android:src="@drawable/images_1" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginLeft="5dp"            android:gravity="center_vertical"            android:orientation="vertical" >            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="vertical" >                <TextView                    android:id="@+id/shopping_name"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:ellipsize="end"                    android:maxLines="2"                    android:text="BF SUMA 美玛 蕃茄精華胶囊 Manergy 90粒 BFSUMA原装进口"                    android:textSize="15sp" />                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_alignParentLeft="true"                    android:orientation="horizontal" >                    <TextView                        android:id="@+id/textView2"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="10dp"                        android:text="¥"                        android:textColor="@color/red"                        android:textSize="15sp" />                    <TextView                        android:id="@+id/shopping_integral"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="2dp"                        android:text="180"                        android:textColor="@color/red"                        android:textSize="15sp" />                    <TextView                        android:id="@+id/textView3"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="3dp"                        android:text="X"                        android:textSize="15sp" />                    <TextView                        android:id="@+id/order_shopping_num_1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="1"                        android:textSize="15sp" />                </LinearLayout>            </LinearLayout>            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:gravity="center" >                <ImageView                    android:id="@+id/shopping_order_jian"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:src="@drawable/jian_jian" />                <TextView                    android:id="@+id/order_shopping_num_2"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:gravity="center"                    android:text="0"                    android:textSize="20sp" />                <ImageView                    android:id="@+id/shopping_order_jia"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:src="@drawable/jia_jia" />            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>




三、接下来就是重点了,适配器我是写在Activity下面的,因为在购物车页面我需要操作对应“+”和“-“事件,其中在item中监听事件第一次做的时候不是很懂,仔细研究了挺久的,详细代码如下,不懂的可以提问的呢,哈哈,简单的购物车功能就是这样子了:

public class ShoppingCartActivity extends Activity {private TextView back;private ListView mListView;private TextView delete_table;private TextView total_order;private TextView update_order;SqliteService sqliteService;List<OrderDetial> list;private List<OrderDetial> intenetlistDetials = new ArrayList<OrderDetial>();ShoppingAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);AppManager.getInstance().addActivity(ShoppingCartActivity.this);setContentView(R.layout.activity_add_to_shopping_car);initView();setListener();}private void initView() {back = (TextView) findViewById(R.id.shopping_detail_product_back);mListView = (ListView) findViewById(R.id.shopping_order_listview1);delete_table = (TextView) findViewById(R.id.shopping_detele_table);total_order = (TextView) findViewById(R.id.shopping_order_total);update_order = (TextView) findViewById(R.id.update_order);sqliteService = new SqliteService(ShoppingCartActivity.this);List<Map<String, String>> mList = new ArrayList<Map<String, String>>();list = new ArrayList<OrderDetial>();OrderDetial orderDetial = null;mList = sqliteService.listOederMaps(null);if (mList == null) {return;}for (int i = 0; i < mList.size(); i++) {Map<String, String> map = mList.get(i);String idString = mList.get(i).get("id").toString();String product_id = mList.get(i).get("product_id").toString();String product_name = mList.get(i).get("product_name").toString();String product_num = mList.get(i).get("product_num").toString();String product_integral = mList.get(i).get("product_integral").toString();String product_pic = mList.get(i).get("product_pic").toString();int id = Integer.parseInt(idString);orderDetial = new OrderDetial(id, product_id, product_num,product_name, product_integral, product_pic, false);list.add(orderDetial);}LogUtils.e("product_list", list.toString());adapter = new ShoppingAdapter(this, list);mListView.setAdapter(adapter);}private void setListener() {back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AppManager.getInstance().finishActivity(ShoppingCartActivity.this);}});delete_table.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubsqliteService.deleteOeder(null);}});}/** * 适配器是我之前自己封装的 * @author zengyi */class ShoppingAdapter extends MyAdapter_CommonAdapter<OrderDetial> {TextView product_num1;TextView product_num2;public ShoppingAdapter(Context context, List<OrderDetial> datas) {super(context, datas);}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {ViewHolder holder = ViewHolder.getHolder(mContext, convertView,parent,com.example.otc.R.layout.item_activity_add_to_shopping_car,position);final OrderDetial bean = list.get(position);((TextView) holder.getView(R.id.shopping_name)).setText(bean.getProduct_name());((TextView) holder.getView(R.id.shopping_integral)).setText(bean.getProduct_integral());ImageView mJia = holder.getView(R.id.shopping_order_jia);ImageView mJian = holder.getView(R.id.shopping_order_jian);product_num1 = ((TextView) holder.getView(R.id.order_shopping_num_1));product_num2 = ((TextView) holder.getView(R.id.order_shopping_num_2));product_num2.setText(bean.getProduct_num());product_num1.setText(bean.getProduct_num());mJia.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {int p_num = Integer.parseInt(list.get(position).getProduct_num());p_num += 1;list.get(position).setProduct_num(p_num + "");// product_num2.setText(p_num + "");adapter.notifyDataSetChanged();}});mJian.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {int p_num = Integer.parseInt(list.get(position).getProduct_num());if (p_num > 1) {p_num--;list.get(position).setProduct_num(p_num + "");adapter.notifyDataSetChanged();} else if (p_num == 1) {p_num = 1;list.get(position).setProduct_num("1");// product_num1.setText(p_num + "");adapter.notifyDataSetChanged();}}});MyImageLoader.getInstance(mContext).displayImage(bean.getProduct_pic(),(ImageView) holder.getView(R.id.shopping_images));boolean ischeck = list.get(position).isIscheck();final CheckBox checkBox = (CheckBox) holder.getView(R.id.shopping_checkBox1);if (ischeck == false) {// checkbox ischeckcheckBox.setChecked(false);} else {checkBox.setChecked(true);}// checkcheckBox.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif (checkBox.isChecked() == true) {checkBox.setChecked(true);intenetlistDetials.add(bean);list.get(position).setIscheck(true);} else {checkBox.setChecked(false);intenetlistDetials.remove(bean);list.get(position).setIscheck(false);}dogeSum(intenetlistDetials);notifyDataSetChanged();}});return holder.getmConvertView();}}/** * 计算总价 * @param intenetlistDetials */private void dogeSum(List<OrderDetial> intenetlistDetials) {double mdoubTotal = 0;if (intenetlistDetials.size()==0) {total_order.setText(0+"");}for (int i = 0; i < intenetlistDetials.size(); i++) {int price = Integer.parseInt(intenetlistDetials.get(i).getProduct_num())* Integer.parseInt(intenetlistDetials.get(i).getProduct_integral());mdoubTotal += price;total_order.setText(mdoubTotal+"");}}}

四、最后运行项目结果如下:



4 0
原创粉丝点击