今天还是因为该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;
List<TypeBean> typeList;
TypeBaseAdapter adapter;
AccountBean accountBean;//保存到数据库中使用的封装类
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountBean=new AccountBean();
accountBean.setTypename("其他");
accountBean.setsImageId(R.mipmap.ic_qita_fs);
}
public BaseRecordFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_outcome, container, false);
initView(view);
setInitTime();
loadDataToGV();
setGVListener();//设置每一项的gradeView的点击事件
return view;
}
/**
* 获取当前时间
*/
private void setInitTime(){
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH: mm");
String time=sdf.format(date);
timeTv.setText(time);
accountBean.setTime(time);
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
accountBean.setYear(year);
accountBean.setMonth(month);
accountBean.setDay(day);
}
/**
* 设置每一项的gradeView的点击事件
*/
private void setGVListener() {
typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.selectPos=i;
adapter.notifyDataSetInvalidated();
TypeBean typeBean = typeList.get(i);
String typename = typeBean.getTypename();
typeTv.setText(typename);
int simageId = typeBean.getSimageId();
typeIv.setImageResource(simageId);
accountBean.setsImageId(simageId);
accountBean.setTypename(typename);
}
});
}
/**
* 给GridView填充数据的方法
*/
private void loadDataToGV(){
typeList=new ArrayList<>();
adapter = new TypeBaseAdapter(getContext(), typeList);
typeGv.setAdapter(adapter);
//获取数据库当中数据源
List<TypeBean> outlist = DBManager.getTypeList(0);
typeList.addAll(outlist);
adapter.notifyDataSetChanged();
}
private void initView(View view) {
keyboardView = view.findViewById(R.id.frag_record_keyboard);
moneyEt = view.findViewById(R.id.frag_record_et_money);
typeIv = view.findViewById(R.id.frag_record_iv);
typeGv = view.findViewById(R.id.frag_record_gv);
typeTv = view.findViewById(R.id.frag_record_tv_type);
beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu);
timeTv = view.findViewById(R.id.frag_record_tv_time);
//显示自定义软键盘
KeyBoardUtils keyBoardUtils = new KeyBoardUtils(keyboardView, moneyEt);
keyBoardUtils.showKeyboard();
//设置接口监听确定按钮被点击了
keyBoardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
@Override
public void onEnsure() {
//获取收入钱数
String moneyStr = moneyEt.getText().toString();
if (!TextUtils.isEmpty(moneyStr)||moneyStr.equals("0")){
getActivity().finish();
return;
}
float money=Float.parseFloat(moneyStr);
accountBean.setMoney(money);
//点击了确定按钮
//获取用户输入的信息
//返回上一级界面
getActivity().finish();
}
});
}
}
最后写了一个账单类来存储用户打算存的账单信息。
package com.open.tally.db;
/**
* 将数据插入到数据库的封装类
*/
public class AccountBean {
int id;
String typename;
int sImageId;
String beizhu;
float money;
String time;
int year;
int month;
int day;
int kind;//收入1,支出0
public AccountBean() {
}
public AccountBean(int id, String typename, int sImageId, String beizhu, float money, String time, int year, int month, int day, int kind) {
this.id = id;
this.typename = typename;
this.sImageId = sImageId;
this.beizhu = beizhu;
this.money = money;
this.time = time;
this.year = year;
this.month = month;
this.day = day;
this.kind = kind;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
public int getsImageId() {
return sImageId;
}
public void setsImageId(int sImageId) {
this.sImageId = sImageId;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章