android软件简约记账app开发day05-记账页面条目代码优化和bug解决
阅读原文时间:2023年07月08日阅读:2

android软件简约记账app开发day05-记账页面条目代码优化和bug解决

今天还是因为该bug又极大的耽误了项目进程,该开发文档都要没有时间来写了。

先说bug吧,在昨天已经实现了页面图标的展示,并且可以左右滑动来切换时支出还时收入页面,可就是在页面上部不显示支出和收入,这让我百思不得其解,翻看项目目录也不知道时那里的问题,我首先试了试Debug来看一下,我打了几个断点,可以当我点击那个晓聪子按钮时,他还让我下载什么东西,我心想我就调试一下,你就每一步就给我走不就完了,还要下载什么东西,真是无语,算了,还是下吧,可我这一点击download,这要下载的东西可多啊,愣是下了得有5分钟,当我下载完后,本以为点击debug按钮后该bug就能简简单单的解决时,知识砂纸擦屁股给我露了一手啊,这来来回回跳转的类得有百十来个,我点啊点,看呀看,是又点不玩,又看不到,来回挑战java类,也还不加载页面,我心想写个日志看看能打印出什么来不,可他就是出不来,什么也输出不了。

其次我就放弃了调试,选择从头梳理一下逻辑,既然能加载,说明记一笔可以点击,并且页面可以跳转,条目可以显示,说明从数据库角度来说没有问题,我相当于又从头看了一下所有昨天写的代码,终于我点进了写的适配器类RecordPagerAdapter我发现在我定义的String[] titles={"支出","收入"};竟然是灰的,这属实让我心头一紧,我立马就发现了

public CharSequence getPageTitle(int position) {
    return titles[position];
}

该类没改return,当然这是改过之后的,这、啊这、啊这这这、哎,我就无语,怎么能有我这么菜的人。

现在看着写起来也就四五百字,这可是四五个小时才发现的。。。。。。

接下来,让我来优化一下代码,稍微、简单的优化一下,把outcomefragment和incomefragment两个类的相同部分抽取一下,写成baserecordfragment类,在用两个类继承一下。

package com.open.tally.frag_record;
​
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
​
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
​
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
​
import com.open.tally.R;
import com.open.tally.db.AccountBean;
import com.open.tally.db.DBManager;
import com.open.tally.db.TypeBean;
import com.open.tally.util.KeyBoardUtils;
​
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
​
/**
 * 记录页面的支出模块
 */
public class BaseRecordFragment extends Fragment {
​
    KeyboardView keyboardView;
    EditText moneyEt;
    ImageView typeIv;
    TextView typeTv, beizhuTv, timeTv;
    GridView typeGv;
 &nbsp; &nbsp;List<TypeBean> typeList;
 &nbsp; &nbsp;TypeBaseAdapter adapter;
 &nbsp; &nbsp;AccountBean accountBean;//保存到数据库中使用的封装类
​
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;public void onCreate(@Nullable Bundle savedInstanceState) {
​
 &nbsp; &nbsp; &nbsp; &nbsp;super.onCreate(savedInstanceState);
​
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean=new AccountBean();
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setTypename("其他");
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setsImageId(R.mipmap.ic_qita_fs);
​
 &nbsp;  }
​
 &nbsp; &nbsp;public BaseRecordFragment() {
 &nbsp; &nbsp; &nbsp; &nbsp;// Required empty public constructor
 &nbsp;  }
 &nbsp; &nbsp;@Override
 &nbsp; &nbsp;public View onCreateView(LayoutInflater inflater, ViewGroup container,
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle savedInstanceState) {
 &nbsp; &nbsp; &nbsp; &nbsp;// Inflate the layout for this fragment
 &nbsp; &nbsp; &nbsp; &nbsp;View view = inflater.inflate(R.layout.fragment_outcome, container, false);
 &nbsp; &nbsp; &nbsp; &nbsp;initView(view);
 &nbsp; &nbsp; &nbsp; &nbsp;setInitTime();
 &nbsp; &nbsp; &nbsp; &nbsp;loadDataToGV();
 &nbsp; &nbsp; &nbsp; &nbsp;setGVListener();//设置每一项的gradeView的点击事件
 &nbsp; &nbsp; &nbsp; &nbsp;return view;
 &nbsp;  }
​
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 获取当前时间
 &nbsp; &nbsp; */
 &nbsp; &nbsp;private void setInitTime(){
 &nbsp; &nbsp; &nbsp; &nbsp;Date date=new Date();
 &nbsp; &nbsp; &nbsp; &nbsp;SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH: mm");
 &nbsp; &nbsp; &nbsp; &nbsp;String time=sdf.format(date);
 &nbsp; &nbsp; &nbsp; &nbsp;timeTv.setText(time);
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setTime(time);
​
​
 &nbsp; &nbsp; &nbsp; &nbsp;Calendar calendar=Calendar.getInstance();
 &nbsp; &nbsp; &nbsp; &nbsp;int year=calendar.get(Calendar.YEAR);
 &nbsp; &nbsp; &nbsp; &nbsp;int month = calendar.get(Calendar.MONTH)+1;
 &nbsp; &nbsp; &nbsp; &nbsp;int day = calendar.get(Calendar.DAY_OF_MONTH);
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setYear(year);
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setMonth(month);
 &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setDay(day);
 &nbsp;  }
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 设置每一项的gradeView的点击事件
 &nbsp; &nbsp; */
 &nbsp; &nbsp;private void setGVListener() {
 &nbsp; &nbsp; &nbsp; &nbsp;typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adapter.selectPos=i;
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adapter.notifyDataSetInvalidated();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TypeBean typeBean = typeList.get(i);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String typename = typeBean.getTypename();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;typeTv.setText(typename);
​
​
​
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int simageId = typeBean.getSimageId();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;typeIv.setImageResource(simageId);
​
​
​
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setsImageId(simageId);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setTypename(typename);
​
​
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }
 &nbsp; &nbsp; &nbsp;  });
 &nbsp;  }
​
​
 &nbsp; &nbsp;/**
 &nbsp; &nbsp; * 给GridView填充数据的方法
 &nbsp; &nbsp; */
 &nbsp; &nbsp;private void loadDataToGV(){
 &nbsp; &nbsp; &nbsp; &nbsp;typeList=new ArrayList<>();
 &nbsp; &nbsp; &nbsp; &nbsp;adapter = new TypeBaseAdapter(getContext(), typeList);
 &nbsp; &nbsp; &nbsp; &nbsp;typeGv.setAdapter(adapter);
 &nbsp; &nbsp; &nbsp; &nbsp;//获取数据库当中数据源
 &nbsp; &nbsp; &nbsp; &nbsp;List<TypeBean> outlist = DBManager.getTypeList(0);
 &nbsp; &nbsp; &nbsp; &nbsp;typeList.addAll(outlist);
 &nbsp; &nbsp; &nbsp; &nbsp;adapter.notifyDataSetChanged();
​
 &nbsp;  }
​
​
 &nbsp; &nbsp;private void initView(View view) {
 &nbsp; &nbsp; &nbsp; &nbsp;keyboardView = view.findViewById(R.id.frag_record_keyboard);
 &nbsp; &nbsp; &nbsp; &nbsp;moneyEt = view.findViewById(R.id.frag_record_et_money);
 &nbsp; &nbsp; &nbsp; &nbsp;typeIv = view.findViewById(R.id.frag_record_iv);
 &nbsp; &nbsp; &nbsp; &nbsp;typeGv = view.findViewById(R.id.frag_record_gv);
 &nbsp; &nbsp; &nbsp; &nbsp;typeTv = view.findViewById(R.id.frag_record_tv_type);
 &nbsp; &nbsp; &nbsp; &nbsp;beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu);
 &nbsp; &nbsp; &nbsp; &nbsp;timeTv = view.findViewById(R.id.frag_record_tv_time);
 &nbsp; &nbsp; &nbsp; &nbsp;//显示自定义软键盘
 &nbsp; &nbsp; &nbsp; &nbsp;KeyBoardUtils keyBoardUtils = new KeyBoardUtils(keyboardView, moneyEt);
 &nbsp; &nbsp; &nbsp; &nbsp;keyBoardUtils.showKeyboard();
 &nbsp; &nbsp; &nbsp; &nbsp;//设置接口监听确定按钮被点击了
 &nbsp; &nbsp; &nbsp; &nbsp;keyBoardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onEnsure() {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//获取收入钱数
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String moneyStr = moneyEt.getText().toString();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!TextUtils.isEmpty(moneyStr)||moneyStr.equals("0")){
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getActivity().finish();
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;float money=Float.parseFloat(moneyStr);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accountBean.setMoney(money);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//点击了确定按钮
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//获取用户输入的信息
​
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//返回上一级界面
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getActivity().finish();
​
​
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }
 &nbsp; &nbsp; &nbsp;  });
 &nbsp;  }
​
}

最后写了一个账单类来存储用户打算存的账单信息。

package com.open.tally.db;
​
/**
 * 将数据插入到数据库的封装类
 */
public class AccountBean {
 &nbsp; &nbsp;int id;
 &nbsp; &nbsp;String typename;
 &nbsp; &nbsp;int sImageId;
 &nbsp; &nbsp;String beizhu;
 &nbsp; &nbsp;float money;
 &nbsp; &nbsp;String time;
 &nbsp; &nbsp;int year;
 &nbsp; &nbsp;int month;
 &nbsp; &nbsp;int day;
 &nbsp; &nbsp;int kind;//收入1,支出0
​
 &nbsp; &nbsp;public AccountBean() {
 &nbsp;  }
​
 &nbsp; &nbsp;public AccountBean(int id, String typename, int sImageId, String beizhu, float money, String time, int year, int month, int day, int kind) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.id = id;
 &nbsp; &nbsp; &nbsp; &nbsp;this.typename = typename;
 &nbsp; &nbsp; &nbsp; &nbsp;this.sImageId = sImageId;
 &nbsp; &nbsp; &nbsp; &nbsp;this.beizhu = beizhu;
 &nbsp; &nbsp; &nbsp; &nbsp;this.money = money;
 &nbsp; &nbsp; &nbsp; &nbsp;this.time = time;
 &nbsp; &nbsp; &nbsp; &nbsp;this.year = year;
 &nbsp; &nbsp; &nbsp; &nbsp;this.month = month;
 &nbsp; &nbsp; &nbsp; &nbsp;this.day = day;
 &nbsp; &nbsp; &nbsp; &nbsp;this.kind = kind;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getId() {
 &nbsp; &nbsp; &nbsp; &nbsp;return id;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setId(int id) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.id = id;
 &nbsp;  }
​
 &nbsp; &nbsp;public String getTypename() {
 &nbsp; &nbsp; &nbsp; &nbsp;return typename;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setTypename(String typename) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.typename = typename;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getsImageId() {
 &nbsp; &nbsp; &nbsp; &nbsp;return sImageId;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setsImageId(int sImageId) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.sImageId = sImageId;
 &nbsp;  }
​
 &nbsp; &nbsp;public String getBeizhu() {
 &nbsp; &nbsp; &nbsp; &nbsp;return beizhu;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setBeizhu(String beizhu) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.beizhu = beizhu;
 &nbsp;  }
​
 &nbsp; &nbsp;public float getMoney() {
 &nbsp; &nbsp; &nbsp; &nbsp;return money;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setMoney(float money) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.money = money;
 &nbsp;  }
​
 &nbsp; &nbsp;public String getTime() {
 &nbsp; &nbsp; &nbsp; &nbsp;return time;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setTime(String time) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.time = time;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getYear() {
 &nbsp; &nbsp; &nbsp; &nbsp;return year;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setYear(int year) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.year = year;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getMonth() {
 &nbsp; &nbsp; &nbsp; &nbsp;return month;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setMonth(int month) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.month = month;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getDay() {
 &nbsp; &nbsp; &nbsp; &nbsp;return day;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setDay(int day) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.day = day;
 &nbsp;  }
​
 &nbsp; &nbsp;public int getKind() {
 &nbsp; &nbsp; &nbsp; &nbsp;return kind;
 &nbsp;  }
​
 &nbsp; &nbsp;public void setKind(int kind) {
 &nbsp; &nbsp; &nbsp; &nbsp;this.kind = kind;
 &nbsp;  }
}