记账本/
RecordActivity.java
public class RecordActivity extends AppCompatActivity { TabLayout tabLayout; ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record); //1.查找控件 tabLayout = findViewById(R.id.record_tabs); viewPager = findViewById(R.id.record_vp); //2.设置ViewPager加载页面 initPager(); } private void initPager() { // 初始化ViewPager页面的集合 List<Fragment>fragmentList = new ArrayList<>(); // 创建收入和支出页面,放置在Fragment当中 OutcomeFragment outFrag = new OutcomeFragment(); //支出 IncomeFragment inFrag = new IncomeFragment(); //收入 fragmentList.add(outFrag); fragmentList.add(inFrag); // 创建适配器 RecordPagerAdapter pagerAdapter = new RecordPagerAdapter(getSupportFragmentManager(), fragmentList); // 设置适配器 viewPager.setAdapter(pagerAdapter); //将TabLayout和ViwePager进行关联 tabLayout.setupWithViewPager(viewPager); } /* 点击事件*/ public void onClick(View view) { switch (view.getId()) { case R.id.record_iv_back: finish(); break; } } }
RecordPagerAdapter.java
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]; } }
lncomeFragment.java
public class IncomeFragment extends BaseRecordFragment { @Override public void loadDataToGV() { super.loadDataToGV(); //获取数据库当中的数据源 List<TypeBean> inlist = DBManager.getTypeList(1); typeList.addAll(inlist); adapter.notifyDataSetChanged(); typeTv.setText("其他"); typeIv.setImageResource(R.mipmap.in_qt_fs); } @Override public void saveAccountToDB() { accountBean.setKind(1); DBManager.insertItemToAccounttb(accountBean); } }
TypeBaseAdapter.java
public class TypeBaseAdapter extends BaseAdapter { Context context; List<TypeBean>mDatas; int selectPos = 0; //选中位置 public TypeBaseAdapter(Context context, List<TypeBean> mDatas) { this.context = context; this.mDatas = mDatas; } @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; } // 此适配器不考虑复用问题,因为所有的item都显示在界面上,不会因为滑动就消失,所有没有剩余的convertView,所以不用复写 @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(context).inflate(R.layout.item_recordfrag_gv,parent,false); //查找布局当中的控件 ImageView iv = convertView.findViewById(R.id.item_recordfrag_iv); TextView tv = convertView.findViewById(R.id.item_recordfrag_tv); //获取指定位置的数据源 TypeBean typeBean = mDatas.get(position); tv.setText(typeBean.getTypename()); // 判断当前位置是否为选中位置,如果是选中位置,就设置为带颜色的图片,否则为灰色图片 if (selectPos == position) { iv.setImageResource(typeBean.getSimageId()); }else{ iv.setImageResource(typeBean.getImageId()); } return convertView; } }
DBOpenHelper.java
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) { } }
TypeBean.java
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; } }
item_recordfrag_gv.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <ImageView android:id="@+id/item_recordfrag_iv" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_qita_fs"/> <TextView android:id="@+id/item_recordfrag_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="其他" android:layout_marginTop="5dp" android:textSize="12sp" android:textColor="@color/grey_7D7D7D" android:gravity="center"/> </LinearLayout>

浙公网安备 33010602011771号