任务02 使用ListView实现购物车界面
6
朱应丽
开始于 2020-03-07 23:19
0 3 156
已截止

任务尚未发布或者你没有权限查看任务内容。

任务讨论

2018011626   任锐媛

(老师我作业没交全

//adapter类

package net.onest.work02;

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

import java.util.ArrayList;
import java.util.List;

public class CartAdapter extends BaseAdapter {
    private Context mContext;
    private List<Good> goods = new ArrayList<>();
    private int itemLayoutRes;

    public CartAdapter(Context mContext, List<Good> goods, int itemLayoutRes) {
        this.mContext = mContext;
        this.goods = goods;
        this.itemLayoutRes = itemLayoutRes;
    }

    @Override
    public int getCount() { //获取数据条数
        if(null != goods) {
            return goods.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) { //获取每个item显示的对象
        if(null != goods) {
            return goods.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) { //获取每个item的id值
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { //获取视图对象
        if(convertView == null) {
            //加载item布局文件
            LayoutInflater inflater = LayoutInflater.from(mContext); //布局填充器类
            convertView = inflater.inflate(itemLayoutRes, null);
        }
        //获取item控件对象的引用
        TextView tvShop = convertView.findViewById(R.id.tv_shop);
        TextView tvDetail = convertView.findViewById(R.id.tv_detail);
        ImageView ivPic = convertView.findViewById(R.id.iv_pic);
        TextView tvPrice = convertView.findViewById(R.id.tv_price);
        final TextView tvNum = convertView.findViewById(R.id.tv_num);
        TextView btnLess = convertView.findViewById(R.id.btn_less);
        TextView btnMore = convertView.findViewById(R.id.btn_more);
        //设置控件内容
        tvShop.setText(goods.get(position).getShopName());
        tvDetail.setText(goods.get(position).getDetail());
        ivPic.setImageResource(goods.get(position).getGoodId());
        tvPrice.setText(goods.get(position).getPrice() + "");
        tvNum.setText(goods.get(position).getNum() + "");

        btnLess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(tvNum.getText().toString());
                if(num > 1) {
                    num--;
                    tvNum.setText(num + "");
                }
            }
        });

        btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(tvNum.getText().toString());
                num++;
                tvNum.setText(num + "");
            }
        });
        return convertView;
    }
}
//商品类

package net.onest.work02;

public class Good {
    private String shopName;
    private String detail;
    private double price;
    private int goodId;
    private int num;

    public Good(String shopName, String detail, double price, int goodId, int num) {
        this.shopName = shopName;
        this.detail = detail;
        this.price = price;
        this.goodId = goodId;
        this.num = num;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getGoodId() {
        return goodId;
    }

    public void setGoodId(int goodId) {
        this.goodId = goodId;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

朱应丽

界面中关于单选按钮的操作不要求实现,只实现任务要求中描述的功能即可

朱应丽

任务已更新

朱应丽

任务已更新