完成了记录支出页面的逻辑编写
此时项目结构
adapter适配器包
RecordPagerAdapter.java
package com.example.myapplication.adapter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class RecordPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList;
String[]titles={"支出","收入"};
public RecordPagerAdapter(@NonNull FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
db包(数据库等相关操作)
DBManager
package com.example.myapplication.db;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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);
// 循环读取图标内容,存储到对象当中
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;
}
}
DBOpenHelper
package com.example.myapplication.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.myapplication.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);
}
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) {
}
}
TypeBean
package com.example.myapplication.db;
/*
*表示收入或者支出具体类型的类
**/
public class TypeBean {
int id;
String typename;//类型名称
int imageId;//未被选中图片id
int simageId;//被选中图片id
int kind; //收入 1 支出 0
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;
}
public TypeBean() {
}
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;
}
}
UniteApp
package com.example.myapplication;
import android.app.Application;
import com.example.myapplication.db.DBManager;
import com.example.myapplication.db.TypeBean;
import java.util.ArrayList;
import java.util.List;
//表示全局应用的类
public class UniteApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化数据库
DBManager.initDB(getApplicationContext());
}
}