将头布局在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; } }