日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

购物车中选择物品结算功能的实现

發(fā)布時間:2024/1/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 购物车中选择物品结算功能的实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、概要

總有學(xué)生要解決 購物車物品結(jié)算問題,下面給出了一個簡單的實現(xiàn):

1.1、購物車中的物品可以修改數(shù)量

1.2、購物車中的物品可以選擇支付【并不對全部物品支付】

1.3、需支付的物品總價,需要顯示,并在用戶修改商品時,實時修改!


2、預(yù)覽圖效果

3、MainActivity代碼

需要事先準備幾個 充當商品的 圖片!

package edu.freshen.shopcat;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView;public class MainActivity extends Activity implements OnItemClickListener {//List<Map<String,Object>>data =new ArrayList(); //List<Map<String,Object>> cdata =new ArrayList();//ListView lv;TextView tv;Button bt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv=(ListView) findViewById(R.id.listView1);tv=(TextView) findViewById(R.id.totalPrice);bt=(Button) findViewById(R.id.payBt);initData();initLv();}private void initLv() {ShopAdapter sa = new ShopAdapter(this, data, R.layout.listview_item);lv.setAdapter(sa);lv.setOnItemClickListener(this);}private void initData() {for (int i = 0; i <4; i++) {Map<String,Object> item =new HashMap();item.put("id", i);item.put("img", R.drawable.item1+i);item.put("name","物品"+i);item.put("price", 10f+5*i);item.put("count", 1);data.add(item);}}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Map item = data.get(arg2);Log.i("Msg", "arg0="+arg0.getClass() +" ,arg1="+arg1.getClass());CheckBox box =(CheckBox) arg1.findViewById(R.id.lv_item_check);if(cdata.contains(item)){cdata.remove(item);box.setChecked(false);}else{cdata.add(item);box.setChecked(true);}sumPrice();}public void sumPrice(){float sum=0;for(Map<String,Object>item : cdata){sum+=(Float)item.get("price")*(Integer)item.get("count");}tv.setText("總價:¥ "+sum);} }


4、MainActivity布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="55dp"></ListView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="5dp"android:layout_alignParentBottom="true"android:background="#DDDDDD"><TextViewandroid:id="@+id/totalPrice"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="總價:" /><Buttonandroid:id="@+id/payBt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="前往支付"/></LinearLayout></RelativeLayout>


5、ListView元素布局文件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:descendantFocusability="blocksDescendants"><CheckBox android:id="@+id/lv_item_check"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:clickable="false"/><ImageViewandroid:id="@+id/lv_item_img"android:layout_width="80dp"android:layout_height="80dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/lv_item_check"android:src="@drawable/item1" /><TextViewandroid:id="@+id/lv_item_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_toRightOf="@+id/lv_item_img"android:layout_alignTop="@id/lv_item_img"android:text="精品頑石"android:textAppearance="?android:attr/textAppearanceMedium" /><TextViewandroid:id="@+id/lv_item_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@id/lv_item_name"android:layout_below="@id/lv_item_name"android:layout_marginTop="5dp"android:text="¥30"android:textAppearance="?android:attr/textAppearanceSmall" /><Button android:id="@+id/lv_item_add"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:padding="2dp"android:text="+" /><EditTextandroid:id="@+id/lv_item_txt"android:layout_width="40dp"android:layout_height="wrap_content"android:layout_toLeftOf="@id/lv_item_add"android:layout_alignBottom="@id/lv_item_add"android:ems="10"android:inputType="number" /><Buttonandroid:id="@+id/lv_item_min"style="?android:attr/buttonStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="2dp"android:layout_alignBottom="@id/lv_item_add"android:layout_toLeftOf="@id/lv_item_txt"android:text="-" /></RelativeLayout>


6、ListView適配器

package edu.freshen.shopcat;import java.util.List; import java.util.Map;import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView;public class ShopAdapter extends BaseAdapter {private MainActivity activity;private List<Map<String,Object>>data;private int layout;public ShopAdapter(MainActivity activity, List<Map<String, Object>> data,int layout) {super();this.activity = activity;this.data = data;this.layout = layout;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn data.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn data.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {final Map<String,Object> item = data.get(arg0);View v =LayoutInflater.from(activity).inflate(layout, null);ImageView iv=(ImageView) v.findViewById(R.id.lv_item_img);iv.setImageResource((Integer) item.get("img"));TextView name =(TextView) v.findViewById(R.id.lv_item_name);name.setText(item.get("name").toString());TextView price =(TextView) v.findViewById(R.id.lv_item_price);price.setText("¥ "+item.get("price").toString());Button addBt= (Button) v.findViewById(R.id.lv_item_add);Button minBt= (Button) v.findViewById(R.id.lv_item_min);final EditText et =(EditText) v.findViewById(R.id.lv_item_txt);et.setText(item.get("count").toString());addBt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {item.put("count", (Integer)item.get("count")+1);et.setText(item.get("count").toString());activity.sumPrice();}});minBt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Integer ct = (Integer)item.get("count")-1;if(ct<=0)ct=0;item.put("count", ct);et.setText(ct.toString());activity.sumPrice();}});return v;}}


總結(jié)

以上是生活随笔為你收集整理的购物车中选择物品结算功能的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。