家庭记账本(六)主界面头布局

  将头布局在ListView中显示,也就是在ListView中添加一个头

复制代码
/*添加ListView头布局*/
    public void addListViewHeader() {
        headerView = getLayoutInflater().inflate(R.layout.item_mainlv_top, null);
        lv_today_info.addHeaderView(headerView);
        /*查找可用控件*/
        tv_top_out = findViewById(R.id.item_mainlv_top_out);
        tv_top_in = findViewById(R.id.item_mainlv_top_tv_in);
        tv_top_budget = findViewById(R.id.item_mainlv_top_tv_budget);
        tv_top_condition = findViewById(R.id.item_mainlv_top_tv_day);
        iv_show = findViewById(R.id.item_mainlv_top_iv_hide);

        headerView.setOnClickListener(this);
        iv_show.setOnClickListener(this);
        tv_top_budget.setOnClickListener(this);
    }
复制代码

  

  那么头布局中的信息如何显示呢?为了能获取头部中的信息,我们需要填写两个数据库中的信息。

复制代码
/*获取某一天支出或者收入的总金额*/
    public static float getSumMoneyOneDay(int year, int month, int day, int kind) {
        float total = 0;

        String sql = "select sum(money) from tb_account where year=? and month=? and day=? and kind=?";
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(day), String.valueOf(kind)});

        if (cursor.moveToFirst()) {
            total = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
        }
        return total;
    }

    /*获取某一月支出或者收入的总金额*/
    public static float getSumMoneyOneMonth(int year, int month, int kind) {
        float total = 0;

        String sql = "select sum(money) from tb_account where year=? and month=? and kind=?";
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(kind)});

        if (cursor.moveToFirst()) {
            total = cursor.getFloat(cursor.getColumnIndex("sum(money)"));
        }
        return total;
    }
复制代码

 

  将数据库中的信息显示出来

复制代码
/*设置顶部布局文本内容的显示*/
    public void setTopTVShow(){
        /*获取今日支出和收入总金额*/
        float income = DBManager.getSumMoneyOneDay(year, month, day, 1);
        float outcome = DBManager.getSumMoneyOneDay(year, month, day, 0);

        String infoOneday = "今日支出 ¥"+outcome+" 收入 ¥"+income;
        tv_top_condition.setText(infoOneday);

        /*获取本月支出和收入总金额*/
        float income2 = DBManager.getSumMoneyOneMonth(year, month, 1);
        float outcome2 = DBManager.getSumMoneyOneMonth(year, month, 0);
        tv_top_in.setText("+"+income2);
        tv_top_out.setText("-"+outcome2);

        /*设置显示预算剩余*/
        float budget = preferences.getFloat("bmoney", 0);
        tv_top_budget.setText(String.valueOf((budget-outcome2)));


    }
复制代码

  我们如果希望可以设置预算就需要另写一个预算的对话框,写法和时间对话框基本一致。

 

  我们在之前看到ListView的头页面有一个眼睛,希望实现明文与密文的切换。

复制代码
  boolean isShow = true;

    public void toggleShow() {
        if (isShow) {
            PasswordTransformationMethod instance1 = PasswordTransformationMethod.getInstance();
            tv_top_in.setTransformationMethod(instance1);
            tv_top_out.setTransformationMethod(instance1);
            tv_top_budget.setTransformationMethod(instance1);
            iv_show.setImageResource(R.mipmap.ic_hide);
            isShow = false;
        } else {
            HideReturnsTransformationMethod instance2 = HideReturnsTransformationMethod.getInstance();
            tv_top_in.setTransformationMethod(instance2);
            tv_top_out.setTransformationMethod(instance2);
            tv_top_budget.setTransformationMethod(instance2);
            iv_show.setImageResource(R.mipmap.ic_show);
            isShow = true;
        }
    }
复制代码

 

posted @   Gazikel  阅读(103)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示