android软件简约记账app开发day04-记账页面条目的代码书写
在前三天我们完成了基本的界面展示,从今天开始,我们进入到后台逻辑代码的编写中,今天开发记账条目的代码
我们在主页面点击记一笔图标后,进入记账页面,记账页面设计要求可左右滑动,左边显示支出右边显示收入,两个页面大体相同,提供图标点击记账,并且使用自定义的键盘样式,且提供备注和时间输入。
我们首先给首页面的记一笔图标添加点击功能
在java代码中,实现跳转
public void onClick(View view) {
switch (view.getId()){
case R.id.main_iv_search:
break;
case R.id.main_btn_exit:
Intent it1=new Intent(this,RecordActivity.class);
startActivity(it1);
break;
case R.id.main_btn_more:
break;
}
}
完成跳转后,开始编写支出页面:
我们在OutcomeFragment类中首先声明如下变量
KeyboardView keyboardView;
EditText moneyEt;
ImageView typeIv;
TextView typeTv, beizhuTv, timeTv;
GridView typeGv;
在onCreateView方法中获取页面View对象,并且初始化View返回------创建initView方法
在initView方法中,我们要将该类的变量赋值,,并且在该方法中显示自定义软键盘,并且设置接口监听确定哪一个按钮被点击
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()
我们要展示项目中的图标,在这里我使用数据的应用,在app安装后,就在设备中创建一个数据库,并创建tablety表来存储item条目数据,我们在OutcomeFragment加载时就先从数据库中查询到条目信息,在展示到页面上,接下来我们编写数据库的代码
首先创建一个类来封装条目对象-----TypeBean
package com.open.tally.db;
/*
* 表示收入或者支出具体类型的类
* */
public class TypeBean {
int id;
String typename; //类型名称
int imageId; //未被选中图片id
int simageId; //被选中图片id
int kind; //收入-1 支出-0
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 getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public int getSimageId() {
return simageId;
}
public void setSimageId(int simageId) {
this.simageId = simageId;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public TypeBean() {
}
public TypeBean(int id, String typename, int imageId, int simageId, int kind) {
this.id = id;
this.typename = typename;
this.imageId = imageId;
this.simageId = simageId;
this.kind = kind;
}
}
然后创建数据库帮助类DBOpenHelper,
在其中提供创建数据库和创建表以及在表中插入提前设计好的条目信息。
package com.open.tally.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.open.tally.R;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(@Nullable Context context) {
super(context,"tally.db" , null, 1);
}
// 创建数据库的方法,只有项目第一次运行时,会被调用
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表示类型的表
String sql = "create table typetb(id integer primary key autoincrement,typename varchar(10),imageId integer,sImageId integer,kind integer)";
db.execSQL(sql);
insertType(db);
//创建记账表
sql = "create table accounttb(id integer primary key autoincrement,typename varchar(10),sImageId integer,beizhu varchar(80),money float," +
"time varchar(60),year integer,month integer,day integer,kind integer)";
db.execSQL(sql);
}
private void insertType(SQLiteDatabase db) {
// 向typetb表当中插入元素
String sql = "insert into typetb (typename,imageId,sImageId,kind) values (?,?,?,?)";
db.execSQL(sql,new Object[]{"其他", R.mipmap.ic_qita,R.mipmap.ic_qita_fs,0});
db.execSQL(sql,new Object[]{"餐饮", R.mipmap.ic_canyin,R.mipmap.ic_canyin_fs,0});
db.execSQL(sql,new Object[]{"交通", R.mipmap.ic_jiaotong,R.mipmap.ic_jiaotong_fs,0});
db.execSQL(sql,new Object[]{"购物", R.mipmap.ic_gouwu,R.mipmap.ic_gouwu_fs,0});
db.execSQL(sql,new Object[]{"服饰", R.mipmap.ic_fushi,R.mipmap.ic_fushi_fs,0});
db.execSQL(sql,new Object[]{"日用品", R.mipmap.ic_riyongpin,R.mipmap.ic_riyongpin_fs,0});
db.execSQL(sql,new Object[]{"娱乐", R.mipmap.ic_yule,R.mipmap.ic_yule_fs,0});
db.execSQL(sql,new Object[]{"零食", R.mipmap.ic_lingshi,R.mipmap.ic_lingshi_fs,0});
db.execSQL(sql,new Object[]{"烟酒茶", R.mipmap.ic_yanjiu,R.mipmap.ic_yanjiu_fs,0});
db.execSQL(sql,new Object[]{"学习", R.mipmap.ic_xuexi,R.mipmap.ic_xuexi_fs,0});
db.execSQL(sql,new Object[]{"医疗", R.mipmap.ic_yiliao,R.mipmap.ic_yiliao_fs,0});
db.execSQL(sql,new Object[]{"住宅", R.mipmap.ic_zhufang,R.mipmap.ic_zhufang_fs,0});
db.execSQL(sql,new Object[]{"水电煤", R.mipmap.ic_shuidianfei,R.mipmap.ic_shuidianfei_fs,0});
db.execSQL(sql,new Object[]{"通讯", R.mipmap.ic_tongxun,R.mipmap.ic_tongxun_fs,0});
db.execSQL(sql,new Object[]{"人情往来", R.mipmap.ic_renqingwanglai,R.mipmap.ic_renqingwanglai_fs,0});
db.execSQL(sql,new Object[]{"其他", R.mipmap.in_qt,R.mipmap.in_qt_fs,1});
db.execSQL(sql,new Object[]{"薪资", R.mipmap.in_xinzi,R.mipmap.in_xinzi_fs,1});
db.execSQL(sql,new Object[]{"奖金", R.mipmap.in_jiangjin,R.mipmap.in_jiangjin_fs,1});
db.execSQL(sql,new Object[]{"借入", R.mipmap.in_jieru,R.mipmap.in_jieru_fs,1});
db.execSQL(sql,new Object[]{"收债", R.mipmap.in_shouzhai,R.mipmap.in_shouzhai_fs,1});
db.execSQL(sql,new Object[]{"利息收入", R.mipmap.in_lixifuji,R.mipmap.in_lixifuji_fs,1});
db.execSQL(sql,new Object[]{"投资回报", R.mipmap.in_touzi,R.mipmap.in_touzi_fs,1});
db.execSQL(sql,new Object[]{"二手交易", R.mipmap.in_ershoushebei,R.mipmap.in_ershoushebei_fs,1});
db.execSQL(sql,new Object[]{"意外所得", R.mipmap.in_yiwai,R.mipmap.in_yiwai_fs,1});
}
// 数据库版本在更新时发生改变,会调用此方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
其次创建数据库管理类DBManager
来管理数据库,用于之后的增删改查操作,今天提供了初始化数据库对象,读取数据库当中的数据,写入内存集合中的两个方法。
package com.open.tally.db;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库管理类
* 进行对表的增删改查
*/
public class DBManager {
private static SQLiteDatabase db;
//初始化数据库对象
public static void initDB(Context context){
DBOpenHelper helper=new DBOpenHelper(context);//得到帮助类对象
db=helper.getWritableDatabase();//得到数据库对象
}
/**
* 读取数据库当中的数据,写入内存集合里
* kind :表示收入或者支出
* */
public static List<TypeBean>getTypeList(int kind){
List<TypeBean>list = new ArrayList<>();
//读取typetb表当中的数据
String sql = "select * from typetb where kind = "+kind;
Cursor cursor = db.rawQuery(sql, null);
System.out.println(cursor);
// 循环读取游标内容,存储到对象当中
while (cursor.moveToNext()) {
String typename = cursor.getString(cursor.getColumnIndexOrThrow("typename"));
int imageId = cursor.getInt(cursor.getColumnIndexOrThrow("imageId"));
int sImageId = cursor.getInt(cursor.getColumnIndexOrThrow("sImageId"));
int kind1 = cursor.getInt(cursor.getColumnIndexOrThrow("kind"));
int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
TypeBean typeBean = new TypeBean(id, typename, imageId, sImageId, kind);
list.add(typeBean);
}
return list;
}
}
最后完成组装
创建了UniteApp类继承Application类,在应用加载时就初始化数据库。
package com.open.tally;
import android.app.Application;
import android.database.Cursor;
import com.open.tally.db.DBManager;
import com.open.tally.db.TypeBean;
import java.util.ArrayList;
import java.util.List;
/**
* 表述全局应用的类
*/
public class UniteApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化数据库
DBManager.initDB(getApplicationContext());
}
}


浙公网安备 33010602011771号