使用ListView实现购物车界面

任务要求:
需要提交Activity类的java源文件代码和布局文件代码(xml文件),注意:提交java源文件代码时选择代码语言为Java,提交布局文件代码时选择代码语言为XML

任务描述:
使用ListView完成如下程序购物车界面,每个商品要显示商铺名称,商品标题、商品价格、商品的数量和商品的图片(这里的图片可以直接放在程序的资源目录中),要求点击某个item的加号时会增加该商品的数量,点击某个item的减号时能够减少数量,但数量至少为1个。界面中所需图片下载地址:链接:https://pan.baidu.com/s/1HY3sntHxL29emqKe3asTHQ 提取码:cdn9
在这里插入图片描述

Product.java:

public class Product {
    private String shopName;
    private String productName;
    private int count;
    private double price;
    private int productPic;

    public Product() {
    }

    public Product(String shopName, String productName, int count, double price, int productPic) {
        this.shopName = shopName;
        this.productName = productName;
        this.count = count;
        this.price = price;
        this.productPic = productPic;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getPrice() {
        return price;
    }

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

    public int getProductPic() {
        return productPic;
    }

    public void setProductPic(int productPic) {
        this.productPic = productPic;
    }

    public String getShopName() {
        return shopName;
    }

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

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private ListView productListView;
    private CartAdapter cartAdapter;
    private List<Product> productList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart_layout);

        initData();//准备数据源
        findViews();//初始化界面
    }
    public void initData(){
        Product p1 = new Product("华为旗舰店",
                "正品Huawei/华为 Mate 9全网通4G双卡双待指纹徕卡双摄智能4G手机",
                2, 2000.0, R.drawable.phone);
        Product p2 = new Product("创意良品",
                "iWALK三合一充电线移动电源一拖二可爱迷你充电宝苹果安卓充电线",
                1,88.0, R.drawable.charger);
        productList.add(p1);
        productList.add(p2);
    }

    public void findViews(){
        productListView = findViewById(R.id.lv_product);
        cartAdapter = new CartAdapter(this,R.layout.cart_item_layout,
                productList);
        productListView.setAdapter(cartAdapter);
        productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("点击",position + "条");
            }
        });
    }
}

CartAdapter.java:

public class CartAdapter extends BaseAdapter {
    private Context mContext;
    private int itemLayoutId;
    private List<Product> products = new ArrayList<>();

    public CartAdapter(Context mContext, int itemLayoutId, List<Product> products) {
        this.mContext = mContext;
        this.itemLayoutId = itemLayoutId;
        this.products = products;
    }

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

    @Override
    public Object getItem(int position) {
        if (null != products){
            return products.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView){
            convertView = LayoutInflater.from(mContext).inflate(itemLayoutId,null);
        }
        //获取每个item的控件对象
        TextView tvShopName = convertView.findViewById(R.id.tv_shop_name);
        ImageView ivPic = convertView.findViewById(R.id.iv_pic);
        TextView tvTitle = convertView.findViewById(R.id.item_title);
        TextView tvPrice = convertView.findViewById(R.id.item_price);
        final TextView tvCount = convertView.findViewById(R.id.tv_count);
        TextView tvSub = convertView.findViewById(R.id.tv_sub);
        TextView tvAdd = convertView.findViewById(R.id.tv_add);

        //设置每个item控件显示的内容
        Product product = products.get(position);
        tvShopName.setText(product.getShopName());
        ivPic.setImageResource(product.getProductPic());
        tvTitle.setText(product.getProductName());
        tvPrice.setText(product.getPrice() + "");
        tvCount.setText(product.getCount() + "");

        //设置数据加、减的监听器
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int current = Integer.parseInt(tvCount.getText().toString());
                tvCount.setText(current + 1 + "");
            }
        });
        tvSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int current = Integer.parseInt(tvCount.getText().toString());
                if (current != 1) {
                    tvCount.setText(current - 1 + "");
                }
            }
        });

        return convertView;
    }
}

cart_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lv_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

cart_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingTop="30dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_shop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/house"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:id="@+id/tv_shop_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="23sp"
            android:gravity="center_vertical"
            android:drawableRight="@drawable/more"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="center_vertical"
            android:text="领券"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <RadioButton
            android:id="@+id/rb_pro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>

        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="130dp"
            android:layout_height="130dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/item_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="bottom">

                <TextView
                    android:id="@+id/item_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/yuan"
                    android:drawablePadding="5dp"
                    android:textSize="18sp"
                    android:textColor="@android:color/holo_red_dark"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_alignParentRight="true">

                    <TextView
                        android:id="@+id/tv_sub"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/border"
                        android:text="-"
                        android:gravity="center"/>
                    <TextView
                        android:id="@+id/tv_count"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/border"
                        android:text="1"
                        android:gravity="center"/>
                    <TextView
                        android:id="@+id/tv_add"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:background="@drawable/border"
                        android:text="+"
                        android:gravity="center"/>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FF小迷糊吖~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值