AS简易购物车

这篇博客介绍了如何在Android Studio中创建一个简单的购物车功能,通过ListView展示商品。文章包含效果展示和关键代码,包括主界面MainActivity的适配器ShopAdapter的使用。
摘要由CSDN通过智能技术生成

@AS—ListView(购物车)

#效果展示:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

#上代码:

主界面MainActivity:

private  int count = 0 ;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private Button submit;
private ListView listView;
private List<Map<String,Object>> list;
private Integer[] imageIDs = {R.mipmap.shaseng,R.mipmap.sunwukong,R.mipmap.tangseng,R.mipmap.zhubajie,R.mipmap.guanyin};
private String[] name = {"悟净","悟空","长老","悟能","菩萨"};
private String[] details = {"悟净:大师兄,师傅又被妖怪抓走了.",
        "悟空:妖怪,吃俺老孙一棒.","长老:来世若有缘分,再与你相恋.",
        "悟能:猴哥,我要回高老庄了.","菩萨:八百里黄泉种满曼陀沙华."};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    //用SharedPreferences实现进入该应用次数
    //获取实例
    sp = getSharedPreferences("info",MODE_PRIVATE);
    editor = sp.edit();
    count = sp.getInt("count",0);
    count++;
    editor.putInt("count",count);
    editor.commit();
    Toast.makeText(this,"这是您的第"+count+"次光临!感谢您的支持!",Toast.LENGTH_SHORT).show();
    submit = findViewById(R.id.btn_submit);
    submit.setOnClickListener(new ClickEvent());

    listView = findViewById(R.id.lv_shopcart);
    list = new ArrayList<>();
    for(int i = 0; i < name.length; i++){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("imageID",imageIDs[i]);
        map.put("tv","物品名称:");
        map.put("name",name[i]);
        map.put("detail",details[i]);
        list.add(map);
    }

    ShopAdapter adapter = new ShopAdapter(this,list);
    listView.setAdapter(adapter);
}

private class ClickEvent implements View.OnClickListener {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String goodsList = "";
        for(int i = 0; i < list.size(); i++) {
            goodsList += ShopAdapter.hasChecked(i)? name[i] + "  ": "";
        }
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("购物清单:")
                .setMessage("你好,你选择了如下商品:\n\n" + " "+goodsList)
                .setPositiveButton("确定", null)
                .show();
    }
}

适配器:

public class ShopAdapter extends BaseAdapter {

private Context context;//运行上下文
private List<Map<String,Object>> data;//商品信息数据
private static boolean[] hasChecked;//记录商品选中状态

public ShopAdapter(Context context, List<Map<String, Object>> data) {
    this.context = context;
    this.data = data;
    hasChecked = new boolean[getCount()];
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

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

@Override
public View getView(final int position, View contentView, ViewGroup parent) {

    final int selectID = position;
    ViewHolder viewHolder = null;
    //第一次加载
    if(contentView == null){
        viewHolder = new ViewHolder();
        //加载布局
        contentView = LayoutInflater.from(context).inflate(R.layout.list_item,null);
        //赋值
        viewHolder.imageView = contentView.findViewById(R.id.imageView);
        viewHolder.tv = contentView.findViewById(R.id.tv);
        viewHolder.tv_name = contentView.findViewById(R.id.tv_name);
        viewHolder.check = contentView.findViewById(R.id.item_cbx);
        viewHolder.btn = contentView.findViewById(R.id.button);
        contentView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) contentView.getTag();
    }

    //设置数据
    viewHolder.imageView.setImageResource((Integer) data.get(position).get("imageID"));
    viewHolder.tv.setText((String) data.get(position).get("tv"));
    viewHolder.tv_name.setText((String) data.get(position).get("name"));
    viewHolder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context)
                    .setIcon((Integer) data.get(position).get("imageID"))
                    .setTitle("物品详情:"+ (String) data.get(position).get("name"))
                    .setMessage((String) data.get(position).get("detail"))
                    .setPositiveButton("确定",null)
                    .create().show();
        }
    });

    //注册多选框状态事件处理
    viewHolder.check.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //记录物品选中状态
            checkedChange(selectID);
        }
    });
    return contentView;
}

public class ViewHolder {
    private ImageView imageView;
    private TextView tv;
    private TextView tv_name;
    private CheckBox check;
    private Button btn;

}

//记录勾选了哪个物品
private void checkedChange(int checkedID){
    hasChecked[checkedID] = !hasChecked[checkedID];
}
//判断物品是否选择
public static boolean hasChecked(int checkedID){
    return hasChecked[checkedID];
}

}

主界面布局:

<LinearLayout
    android:layout_width="409dp"
    android:layout_height="729dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:gravity="center"
        android:textSize="25sp"
        android:text="毛绒玩具公仔购物车" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="280dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:text="提交"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:text="商品列表 :" />

    <ListView
        android:id="@+id/lv_shopcart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp" />


</LinearLayout>

如图所示:

在这里插入图片描述
item_list:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_launcher_foreground" />

<LinearLayout
    android:layout_width="170dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="物品名称:" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="20sp" />
</LinearLayout>

<CheckBox
    android:id="@+id/item_cbx"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_weight="1"
    android:text=" " />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="商品详情" />

如图所示:
水平布局(horizontal)

附加图片

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值