新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)購(gòu)物車(chē)功能的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的麻栗坡網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; public class MainActivity extends AppCompatActivity implements CartAdapter.RefreshPriceInterface ,View.OnClickListener{ private ListView listView; private CheckBox cb_check_all; private TextView tv_total_price; private TextView tv_delete; private TextView tv_go_to_pay; private CartAdapter adapter; private double totalPrice = 0.00; private int totalCount = 0; private List> goodsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDate(); } //控制價(jià)格展示 private void priceControl(Map pitchOnMap){ totalCount = 0; totalPrice = 0.00; for(int i=0;i pitchOnMap) { priceControl(pitchOnMap); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.all_chekbox: AllTheSelected(); break; case R.id.tv_go_to_pay: if(totalCount<=0){ Toast.makeText(this,"請(qǐng)選擇要付款的商品~",Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this,"錢(qián)就是另一回事了~",Toast.LENGTH_SHORT).show(); break; case R.id.tv_delete: if(totalCount<=0){ Toast.makeText(this,"請(qǐng)選擇要?jiǎng)h除的商品~",Toast.LENGTH_SHORT).show(); return; } checkDelete(adapter.getPitchOnMap()); break; } } //刪除操作 private void checkDelete(Map map){ List > waitDeleteList=new ArrayList<>(); Map waitDeleteMap =new HashMap<>(); for(int i=0;i 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){//已經(jīng)全選,做反選 for(int i=0;i (); for(int i=0;i<10;i++){ HashMap map=new HashMap<>(); map.put("id",(new Random().nextInt(10000)%(10000-2900+2900) + 2900)+""); map.put("name","購(gòu)物車(chē)?yán)锏牡?+(i+1)+"件商品"); map.put("type",(i+20)+"碼"); map.put("price",(new Random().nextInt(100)%(100-29+29) + 29)+""); map.put("count",(new Random().nextInt(10)%(10-1+1) + 1)+""); goodsList.add(map); } initView(); } }
CartAdapter布局:
import android.content.Context; 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.TextView; import android.widget.Toast; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by lipeng * 2017/6/5. */ public class CartAdapter extends BaseAdapter { private Context context; private List> dataList; private ViewHolder vh; private Map pitchOnMap; private RefreshPriceInterface refreshPriceInterface; public CartAdapter(Context context, List > list){ this.context=context; this.dataList=list; pitchOnMap=new HashMap<>(); for(int i=0;i 0){ if(pitchOnMap.get(dataList.get(position).get("id"))==0)vh.checkBox.setChecked(false); else vh.checkBox.setChecked(true); HashMap map=dataList.get(position); vh.name.setText(map.get("name")); vh.num.setText(map.get("count")); vh.type.setText(map.get("type")); vh.price.setText("¥ "+(Double.valueOf(map.get("price")) * Integer.valueOf(map.get("count")))); vh.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final int index=position; if(((CheckBox)view).isChecked())pitchOnMap.put(dataList.get(index).get("id"),1);else pitchOnMap.put(dataList.get(index).get("id"),0); refreshPriceInterface.refreshPrice(pitchOnMap); } }); vh.reduce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final int index=position; dataList.get(index).put("count",(Integer.valueOf(dataList.get(index).get("count"))-1)+""); if(Integer.valueOf(dataList.get(index).get("count"))<=0){ //可提示是否刪除該商品,確定就remove,否則就保留1 String deID=dataList.get(index).get("id"); dataList.remove(index); pitchOnMap.remove(deID); } notifyDataSetChanged(); refreshPriceInterface.refreshPrice(pitchOnMap); } }); vh.add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final int index=position; dataList.get(index).put("count",(Integer.valueOf(dataList.get(index).get("count"))+1)+""); if(Integer.valueOf(dataList.get(index).get("count"))>15){ //15為庫(kù)存可選擇上限 Toast.makeText(context,"已達(dá)庫(kù)存上限~",Toast.LENGTH_SHORT).show(); return; } notifyDataSetChanged(); refreshPriceInterface.refreshPrice(pitchOnMap); } }); } return view; } public Map getPitchOnMap(){ return pitchOnMap; } public void setPitchOnMap(Map pitchOnMap){ this.pitchOnMap=new HashMap<>(); this.pitchOnMap.putAll(pitchOnMap); } public interface RefreshPriceInterface{ void refreshPrice(Map pitchOnMap); } public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface){ this.refreshPriceInterface=refreshPriceInterface; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public int getCount() { if (dataList != null) { return dataList.size(); } else { return 0; } } class ViewHolder{ CheckBox checkBox; ImageView icon; TextView name,price,num,type,reduce,add; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前文章:Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
本文URL:http://fisionsoft.com.cn/article/iejspo.html