最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android实现简单购物车功能
时间:2022-06-25 23:16:05 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了Android实现购物车功能的具体代码,供大家参考,具体内容如下
MainActivity布局:
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();
}
//控制价格展示
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 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){//已经全选,做反选
for(int i=0;i();
for(int i=0;i map=new HashMap();
map.put("id",(new Random().nextInt(10000)%(10000-2900+2900) + 2900)+"");
map.put("name","购物车里的第"+(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;i0){
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"))15){
//15为库存可选择上限
Toast.makeText(context,"已达库存上限~",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;
}
}
相关文章
- dnf神界版本搬砖图推荐 神界版本怎么搬砖 11-29
- 蓝色星原旅谣薄纱流派搭配方法 11-29
- dnf神界版本元素师装备搭配毕业攻略 装备怎么选 11-29
- loselife小女孩好感度提升方法 好感度怎么加 11-29
- 流放之路S24夺宝异界策略收益统计攻略 11-29
- 我的H暑假存档位置攻略 存档在哪 11-29