简单的购物车

简单的实现购物车,效果如下:

简单的购物车_第1张图片public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView listView;
    private List<Product> datas;
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setListener();
    }

    private void initData() {
        datas=new ArrayList<Product>();
        Log.i("+++++++++++++++","");
        Product product;
        for (int i=0;i<30;i++){
            product=new Product();
            product.setName("商品"+(i+1)+"单价"+(i+1));
            product.setNum(1);
            product.setPrice(i+1);
            Log.i("-----商品名字------",product.getName());
            datas.add(product);
        }
        Log.i("data-------------------",datas.size()+"");
        myAdapter=new MyAdapter(datas,MainActivity.this);
        listView.setAdapter(myAdapter);
        myAdapter.setOnAddNum(this);
        myAdapter.setOnSubNum(this);
        listView.setOnItemClickListener(this);
    }

    private void setListener() {

    }

    private void initView() {
        listView = ((ListView) findViewById(R.id.listView));
    }

    @Override
    public void onClick(View view) {
        Object tag=view.getTag();
        switch (view.getId()){
            case R.id.item_btn_add://点击添加数量按钮 进行相应的处理
                if (tag!=null&&tag instanceof Integer){//解决问题: 如何知道你点击的按钮是哪一个列表项中的 通过Tag的position
                    int position=(Integer)tag;
                    //更改集合中的数据
                    int num=datas.get(position).getNum();
                    num++;
                    datas.get(position).setNum(num);//修改集合中商品数量
                    datas.get(position).setPrice((position+1)*num);//修改集合中该商品总价 数量*单价
                    //解决问题:点击某个按钮的时候,如果俩表象所虚的数据改变了 如何更新ui
                    myAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.item_btn_sub:
                //点击减少数量按钮 处理
                //获取Adapter中设置的Tag
                if(tag!=null&&tag instanceof Integer){
                    int position=(Integer)tag;
                    //更改集合的数据
                    int num=datas.get(position).getNum();
                    if (num>0){
                        num--;
                        datas.get(position).setNum(num);
                        datas.get(position).setPrice((position+1) * num);
                        Log.i("********价钱*********",position * num+"");
                        myAdapter.notifyDataSetChanged();
                    }
                }

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(MainActivity.this, "点击了第"+i+"个列表项", Toast.LENGTH_SHORT).show();
    }
}

package www.doolii.cn.shoopingcar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

/**
 * Created by lzq on 2015/11/23.
 */
public class MyAdapter extends BaseAdapter {
    List<Product> list;
    Context context;
    private View.OnClickListener onAddNum;//加商品数量接口
    private View.OnClickListener onSubNum;//减商品数量接口
    public MyAdapter(List<Product> list,Context context) {
        this.list=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        int ret=0;
        if (list!=null){
            ret=list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    //设置接口方法
    public void setOnAddNum(View.OnClickListener onAddNum){
        this.onAddNum=onAddNum;
    }  public void setOnSubNum(View.OnClickListener onSubNum){
        this.onSubNum=onSubNum;
    }
    //内部类
    private static class ViewHolder{
        private TextView item_product_name,item_product_num,item_product_price;
        private ImageButton item_btn_add,item_btn_sub;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=null;
        if (view!=null){
            v=view;
        }else{
            v= LayoutInflater.from(context).inflate(R.layout.list_item,null);

        }
        ViewHolder holder=(ViewHolder)v.getTag();
        if (holder==null){
            holder=new ViewHolder();
            holder.item_product_name=(TextView)v.findViewById(R.id.item_product_name);
            holder.item_product_num=(TextView)v.findViewById(R.id.item_product_num);
            holder.item_product_price=(TextView)v.findViewById(R.id.item_product_price);
            //设置接口回调 注意参数不是上下文 他需要ListView所在的Activity或fragment处理接口回调方法
            holder.item_btn_add=(ImageButton)v.findViewById(R.id.item_btn_add);
            holder.item_btn_add.setOnClickListener(onAddNum);
            holder.item_btn_sub=(ImageButton)v.findViewById(R.id.item_btn_sub);
            holder.item_btn_sub.setOnClickListener(onSubNum);
        }
        holder.item_product_name.setText(list.get(i).getName());
        holder.item_product_num.setText(list.get(i).getNum()+"");
        holder.item_product_price.setText(list.get(i).getPrice()+"");
        //设置Tag,用于判断用户当前点击的哪一个列表项的按钮,解决问题:如何知道你点击的那妞是哪一个列表项中的
        holder.item_btn_add.setTag(i);
        holder.item_btn_sub.setTag(i);
        v.setTag(holder);
        return v;
    }
}

adapter:

package www.doolii.cn.shoopingcar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

/**
 * Created by lzq on 2015/11/23.
 */
public class MyAdapter extends BaseAdapter {
    List<Product> list;
    Context context;
    private View.OnClickListener onAddNum;//加商品数量接口
    private View.OnClickListener onSubNum;//减商品数量接口
    public MyAdapter(List<Product> list,Context context) {
        this.list=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        int ret=0;
        if (list!=null){
            ret=list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    //设置接口方法
    public void setOnAddNum(View.OnClickListener onAddNum){
        this.onAddNum=onAddNum;
    }  public void setOnSubNum(View.OnClickListener onSubNum){
        this.onSubNum=onSubNum;
    }
    //内部类
    private static class ViewHolder{
        private TextView item_product_name,item_product_num,item_product_price;
        private ImageButton item_btn_add,item_btn_sub;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=null;
        if (view!=null){
            v=view;
        }else{
            v= LayoutInflater.from(context).inflate(R.layout.list_item,null);

        }
        ViewHolder holder=(ViewHolder)v.getTag();
        if (holder==null){
            holder=new ViewHolder();
            holder.item_product_name=(TextView)v.findViewById(R.id.item_product_name);
            holder.item_product_num=(TextView)v.findViewById(R.id.item_product_num);
            holder.item_product_price=(TextView)v.findViewById(R.id.item_product_price);
            //设置接口回调 注意参数不是上下文 他需要ListView所在的Activity或fragment处理接口回调方法
            holder.item_btn_add=(ImageButton)v.findViewById(R.id.item_btn_add);
            holder.item_btn_add.setOnClickListener(onAddNum);
            holder.item_btn_sub=(ImageButton)v.findViewById(R.id.item_btn_sub);
            holder.item_btn_sub.setOnClickListener(onSubNum);
        }
        holder.item_product_name.setText(list.get(i).getName());
        holder.item_product_num.setText(list.get(i).getNum()+"");
        holder.item_product_price.setText(list.get(i).getPrice()+"");
        //设置Tag,用于判断用户当前点击的哪一个列表项的按钮,解决问题:如何知道你点击的那妞是哪一个列表项中的
        holder.item_btn_add.setTag(i);
        holder.item_btn_sub.setTag(i);
        v.setTag(holder);
        return v;
    }
}


你可能感兴趣的:(简单的购物车)