首页 > 其他分享 >2022-11-17学习内容

2022-11-17学习内容

时间:2022-11-17 23:22:28浏览次数:41  
标签:11 count findViewById 17 tv 2022 import id view

1.案例-购物车-购物车列表展示

1.1ShoppingCartActivity.java

package com.example.chapter06;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.chapter06.database.ShoppingDBHelper;
import com.example.chapter06.entity.CartInfo;
import com.example.chapter06.entity.GoodsInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_count;
    private LinearLayout ll_cart;
    private ShoppingDBHelper mDBHelper;
    // 声明一个购物车中的商品信息列表
    private List<CartInfo> mCartList;
    // 声明一个根据商品编号查找商品信息的映射,把商品信息缓存起来,这样不用每一次都查询数据库
    private Map<Integer, GoodsInfo> mGoodsMap = new HashMap<>();
    private TextView tv_total_price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("购物车");
        ll_cart = findViewById(R.id.ll_cart);
        tv_total_price = findViewById(R.id.tv_total_price);

        tv_count = findViewById(R.id.tv_count);
        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));

        mDBHelper = ShoppingDBHelper.getInstance(this);

        findViewById(R.id.iv_back).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showCart();
    }

    // 展示购物车中的商品列表
    private void showCart() {
        // 移除下面的所有子视图
        ll_cart.removeAllViews();
        // 查询购物车数据库中所有的商品记录
        mCartList = mDBHelper.queryAllCartsInfo();
        if (mCartList.size() == 0) {
            return;
        }
        for (CartInfo info : mCartList) {
            // 根据商品编号查询商品数据库中的商品记录
            GoodsInfo goods = mDBHelper.queryGoodsInfoById(info.goodsId);
            mGoodsMap.put(info.goodsId, goods);

            // 获取布局文件item_cart.xml的根视图
            View view = LayoutInflater.from(this).inflate(R.layout.item_cart, null);
            ImageView iv_thumb = view.findViewById(R.id.iv_thumb);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            TextView tv_count = view.findViewById(R.id.tv_count);
            TextView tv_price = view.findViewById(R.id.tv_price);
            TextView tv_sum = view.findViewById(R.id.tv_sum);

            iv_thumb.setImageURI(Uri.parse(goods.picPath));
            tv_name.setText(goods.name);
            tv_desc.setText(goods.description);
            tv_count.setText(String.valueOf(info.count));
            tv_price.setText(String.valueOf((int)goods.price));
            // 设置商品总价
            tv_sum.setText(String.valueOf((int)info.count * goods.price));

            // 往购物车列表添加该商品行
            ll_cart.addView(view);
        }

        // 重新计算购物车中的商品总金额
        refreshTotalPrice();
    }

    // 重新计算购物车中的商品总金额
    private void refreshTotalPrice() {
        int totalPrice = 0;
        for (CartInfo info : mCartList) {
            GoodsInfo goods = mGoodsMap.get(info.goodsId);
            totalPrice += goods.price * info.count;
        }
        tv_total_price.setText(String.valueOf(totalPrice));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                // 点击了返回图标
                // 关闭当前页面
                finish();
                break;
        }
    }
}

1.2效果:

 

标签:11,count,findViewById,17,tv,2022,import,id,view
From: https://www.cnblogs.com/pingfanliliang/p/16898245.html

相关文章

  • 17.
    2)pandas中NoneSnp.nan的操作isnull()notnull()dropna():过滤丢失数据fillna():填充丢失数据(1)判断函数isnull().notnull()(2)过滤函数 -------......
  • 20221117-python-条件判断
    1.浅拷贝与深拷贝        2.分支语句   ......
  • cobra 2022 简明教程 - 8 个知识点快速上手
    cobra是命令行程序开发的辅助工具,官方源码在​​https://github.com/spf13/cobra​​,知名程序k8s/githubcli/hugo均使用其开发,但由于网上大多数教程过时且繁琐,提......
  • 2022.11.17
    ###noip模拟暴力都好高明啊,感觉又是除了暴力无甚可讲的一天。。。##出错点t4:又犯老毛病,长一点复杂一点就读不懂,没写暴力,然而这题暴力分给得很足。。。##过程分析......
  • 迟到的windows11虚拟机初体验
    迟到的windows11虚拟机初体验近日微软发布了继win10“最后一个版本”的windows11,相信很多小伙伴早已一睹为快,而我呢当时也是立即就了解到这个事情了,只不过一直没着急真正的......
  • 2022年11月系统架构设计师考试知识点分布
    2022年11月系统架构设计师考试知识点分布薛大龙 邹月平施游 1、上午知识点分布表1是按题号对应的考试内容。表1按试题号分布的考查内容试题号知识点试题号知识点1云计......
  • 【1117】
    792. 匹配子序列的单词数  中等   相关企业给定字符串 s 和字符串数组 words,返回  words[i] 中是s的子序列的单词个......
  • 百题_每日一题Day11
    打印所有的水仙花数。例如:1^3+5^3+3^3=1531.字符串取数:ifint(i)==int(str(i)[0])**3+int(str(i)[1])**3+int(str(i)[2])**3:--这里利用字符串取数,规避整数位数......
  • 2022NOIP A层联测29 A B C D(特殊数列 数进制数 最短路之和 一笔画)
    T1[状态压缩DP]给出\(n,m,p,q,r\),求长度是n,值域在\([1,m]\)之间的序列个数,满足\(\exists1\leqx<y<z<k\leqn,\)\(sum(x,y-1)=p,sum(y,z-1)=q,sum(z,k)=r\).(n<=50,max(p......
  • 闲话 22.11.17
    闲话关于我的博客……我该写还是会写的而且应该不会少博客日更大概只有在改不出模拟赛的题时才会断而且最近写洛谷题解勤了些所以最近博客还长了点(关于为什么要写…......