Android 仿京東購物車

因爲項目中需要用到購物車,在網上找了很久,一直沒有找到一個滿意的購物車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;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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() {

			@Override
			public void onClick(View v) {

				AppManager.getInstance().finishActivity(
						ShoppingCartActivity.this);

			}
		});

		delete_table.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				sqliteService.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);

		}

		@Override
		public 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() {
				@Override
				public 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() {

				@Override
				public 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 ischeck
				checkBox.setChecked(false);
			} else {
				checkBox.setChecked(true);
			}
			// check
			checkBox.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if (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+"");
		}
		
		
	}
	
}

四、最後運行項目結果如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章