之前我们已经实现可以将账单信息录入数据库中。在之前的设计中,我们要想在主界面显示这些保存的信息,那么现在首先来完善主界面,让主界面可以正常显示。
之前的编写时我们已经知道,主界面大部分是有一个ListView来显示。首先我们就需要创建一个这个的适配器。
public class AccountAdapter extends BaseAdapter { Context context; List<AccountBean> mDatas; int year, month, day; public AccountAdapter(Context context, List<AccountBean> mDatas) { this.context = context; this.mDatas = mDatas; Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH)+1; day = calendar.get(Calendar.DAY_OF_MONTH); } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_mainlv, parent, false); viewHolder = new ViewHolder(convertView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } AccountBean bean = mDatas.get(position); viewHolder.iv_type.setImageResource(bean.getsImageId()); viewHolder.tv_type.setText(bean.getTypename()); viewHolder.tv_mark.setText(bean.getMark()); if (bean.getKind() == 1) { viewHolder.tv_money.setText(" +"+bean.getMoney()); } else { viewHolder.tv_money.setText(" -"+bean.getMoney()); } if (bean.getYear()==year&&bean.getMonth()==month&&day==bean.getDay()) { String time = bean.getTime().split(" ")[1]; viewHolder.tv_time.setText("今天 "+time); } else { viewHolder.tv_time.setText(bean.getTime()); } return convertView; } class ViewHolder { ImageView iv_type; TextView tv_type, tv_mark, tv_time, tv_money; public ViewHolder(View view) { iv_type = view.findViewById(R.id.item_mainlv_iv); tv_type = view.findViewById(R.id.item_mainlv_title); tv_mark = view.findViewById(R.id.item_mainlv_tv_mark); tv_time = view.findViewById(R.id.item_mainlv_tv_time); tv_money = view.findViewById(R.id.item_mainlv_tv_money); } } }
使用ViewHolder实现当数据多时实现向下翻阅。
/*设置适配器,加载每一行的数据*/ adapter = new AccountAdapter(this, mDats); lv_today_info.setAdapter(adapter);
在onResume中加载需要显示的数据。onResume是在获取焦点时。
/*当Activity获取焦点时会调用的方法*/ @Override protected void onResume() { super.onResume(); loadDBData(); }
添加方法在数据库中添加查找所有账单信息的方法。
/*获取记账表中某一天的所有支出或者记账情况*/ public static List<AccountBean> getAccountListOneDay(int year, int month, int day){ ArrayList<AccountBean> list = new ArrayList<>(); String sql = "select * from tb_account where year=? and month=? and day=? order by id desc"; Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(day)}); /*遍历符合要求的每一行数据*/ while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String typename = cursor.getString(cursor.getColumnIndex("typename")); String mark = cursor.getString(cursor.getColumnIndex("mark")); String time = cursor.getString(cursor.getColumnIndex("time")); int sImageId = cursor.getInt(cursor.getColumnIndex("sImageId")); int kind = cursor.getInt(cursor.getColumnIndex("kind")); float money = cursor.getFloat(cursor.getColumnIndex("money")); AccountBean accountBean = new AccountBean(id, typename, sImageId, mark, money, time, year, month, day, kind); list.add(accountBean); } return list; }
public void loadDBData() { List<AccountBean> list = DBManager.getAccountListOneDay(year, month, day); mDats.clear(); mDats.addAll(list); adapter.notifyDataSetChanged(); }