Fastlib库是一个多功能的android第三方库。为了能让使用者尽量使用所有的功能,我写一个记帐本的demo来让大家提升对Fastlib使用能力
看之前请先预习一下
数据库功能FastDatabase基础使用
数据库适配器基于FastDatabase适配器
封装弹窗多工具
Activity封装FastActivity
这个记账本要实现
public class Record implements Serializable{ //主键并且自动增长 @Database(keyPrimary = true,autoincrement = true) public int id; public double money; public long date; //使用long类型更好使用数据库过滤 public String title; public String description; }
/** * 创建消费或收入记录 */ @ContentView(R.layout.act_create_record) public class CreateRecordActivity extends FastActivity{ public static final String ARG_SEL_RECORD="record"; //如果有这个参数说明是修改 @Bind(R.id.titleBar) TitleBar mTitleBar; @Bind(R.id.description) EditText mDescription; @Bind(R.id.title) EditText mTitle; @Bind(R.id.money) EditText mMoney; @Bind(R.id.type) Spinner mAccountType; @Bind(R.id.time) TextView mTime; @LocalData(ARG_SEL_RECORD) Record mRecord; Calendar mCalendar=Calendar.getInstance(); @Override protected void alreadyPrepared(){ mTime.setText(String.format(Locale.getDefault(),"%d年%d月%d日",mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH))); mTitleBar.setOnLeftClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); mTitleBar.setOnRightClickListener(new View.OnClickListener() { @Override public void onClick(View v) { createRecord(); } }); initIfChangeData(); } /** * 如果是修改数据,将老数据填充 */ private void initIfChangeData(){ if(mRecord!=null){ DecimalFormat format=new DecimalFormat("#.##"); mTime.setText(String.format(Locale.getDefault(),"%d年%d月%d日",mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH)+1,mCalendar.get(Calendar.DAY_OF_MONTH))); mDescription.setText(mRecord.description); mMoney.setText(format.format(mRecord.money)); mTitle.append(mRecord.title); } } /** * 创建或修改记录 */ private void createRecord(){ int accountType=mAccountType.getSelectedItemPosition(); //0为收入 1为支出 String title=mTitle.getText().toString(); String description=mDescription.getText().toString(); String money=mMoney.getText().toString(); if(TextUtils.isEmpty(title)){ N.showShort(this,"没有输入标题"); return; } if(TextUtils.isEmpty(money)){ N.showShort(this,"没有输入钱"); return; } Record record=mRecord==null?new Record():mRecord; record.money=Double.parseDouble(money)*(accountType==0?1:-1); record.date=mCalendar.getTimeInMillis(); record.title=title; record.description=description; FastDatabase.getDefaultInstance(this).saveOrUpdate(record); //存到数据库成功后发送事件,关闭当前页面 EventObserver.getInstance().sendEvent(new ERefreshList()); finish(); } /** * 修改记录时间 */ @Bind(R.id.time) private void changeTime(){ DatePickerDialog dialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){ mCalendar.set(year,monthOfYear,dayOfMonth); mTime.setText(String.format(Locale.getDefault(),"%d年%d月%d日",year,monthOfYear+1,dayOfMonth)); } },mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } }
/** * 记录适配器 */ public class RecordAdapter extends DatabaseAdapter{ private long mFilterDate; //某个年月的毫秒值 public RecordAdapter(Context context){ super(context,Record.class,R.layout.item,false); Calendar calendar=Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); mFilterDate=calendar.getTimeInMillis(); refresh(); } @Override protected void binding(int position, Record data, OldViewHolder holder){ DecimalFormat decimalFormat=new DecimalFormat("#.##"); //最多保留两位 TextView price=holder.getView(R.id.money); holder.setText(R.id.title,data.title); holder.setText(R.id.date,TimeUtil.dateToString(new Date(data.date),"MM月dd日")); //显示月日 if(data.money >0) { price.setText("+"+decimalFormat.format(data.money)); price.setTextColor(mContext.getResources().getColor(R.color.green_600)); } else { price.setText(decimalFormat.format(data.money)); price.setTextColor(mContext.getResources().getColor(R.color.red_600)); } } @Override protected FastDatabase generateDB(boolean isRefresh){ return FastDatabase.getDefaultInstance(mContext) .setFilter(And.condition(Condition.bigger("date",getStartTime())) //增加过滤条件起始时间 .and(Condition.smaller("date",getEndTime()))); //增加过滤条件结束时间 } private long getStartTime(){ Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date(mFilterDate)); System.out.println("过滤起点:"+TimeUtil.dateToString(calendar.getTime(),"yyyy年MM月dd日")); return calendar.getTimeInMillis(); } private long getEndTime(){ Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date(mFilterDate)); if(calendar.get(Calendar.MONTH)>=11){ calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+1); calendar.set(Calendar.MONTH,0); } else calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1); System.out.println("过滤终点:"+TimeUtil.dateToString(calendar.getTime(),"yyyy年MM月dd日")); return calendar.getTimeInMillis(); } /** * 设置过滤时间.自动取毫秒值转换后的x月1号到下一个月1号 * @param date */ public void setFilterDate(long date){ mFilterDate=date; refresh(); } }
/** * 主Activity同时也是记录列表 */ @ContentView(R.layout.act_main) public class MainActivity extends FastActivity implements AdapterView.OnItemClickListener{ @Bind(R.id.titleBar) TitleBar mTitleBar; @Bind(R.id.list) ListView mListView; @Bind(R.id.date) TextView mDate; @Bind(R.id.incomeCount) TextView mIncomeCount; @Bind(R.id.expenseCount) TextView mExpenseCount; @Bind(R.id.remain) TextView mRemain; RecordAdapter mAdapter; Calendar mCalendar; @Override protected void alreadyPrepared(){ mListView.setAdapter(mAdapter=new RecordAdapter(this)); mListView.setOnItemClickListener(this); mTitleBar.setOnRightClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ startActivity(CreateRecordActivity.class); } });//初始化成当前月份1号0点0分0秒 mCalendar=Calendar.getInstance(); mCalendar.set(Calendar.DAY_OF_MONTH,1); mCalendar.set(Calendar.HOUR_OF_DAY,0); mCalendar.set(Calendar.MINUTE,0); mCalendar.set(Calendar.SECOND,0);refreshDataView(); } /** * 刷新数据 */ private void refreshDataView(){ DecimalFormat decimalFormat=new DecimalFormat("#.##");Record incomeRecord=FastDatabase.getDefaultInstance(this) //请求数据库 .setFunctionCommand(new FunctionCommand("money", FunctionType.SUM, //数据库叠加函数 And.condition(Condition.bigger("money",0)) //增加过滤条件大于0元 .and(Condition.bigger("date",mCalendar.getTimeInMillis())) //增加过滤条件大于当前月份1号 .and(Condition.smaller("date",getEndTime())))) //增加过滤条件小于下个月1号 .getFirst(Record.class);Record expenseRecord=FastDatabase.getDefaultInstance(this) .setFunctionCommand(new FunctionCommand("money",FunctionType.SUM,And.condition(Condition.smaller("money",0)) .and(Condition.bigger("date",mCalendar.getTimeInMillis())) .and(Condition.smaller("date",getEndTime())))) .getFirst(Record.class); double incomeCount=incomeRecord==null?0:incomeRecord.money; double expenseCount=expenseRecord==null?0:expenseRecord.money; mIncomeCount.setText("收入+"+decimalFormat.format(incomeCount)); mExpenseCount.setText("支出"+decimalFormat.format(expenseCount)); mRemain.setText("盈余:"+(incomeCount+expenseCount)); mDate.setText(String.format(Locale.getDefault(),"%d年%d月",mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH)+1)); } @Override public void onItemClick(AdapterView parent, View view, final int position, long id) { final Record record=mAdapter.getItem(position); FastDialog.showListDialog(new String[]{"修改","删除"}).show(getSupportFragmentManager(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ //0修改,1删除 if(which==0){ Intent intent=new Intent(MainActivity.this,CreateRecordActivity.class); intent.putExtra(CreateRecordActivity.ARG_SEL_RECORD,record); startActivity(intent); } else{ FastDialog.showMessageDialog("确定删除?",false).show(getSupportFragmentManager(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { FastDatabase.getDefaultInstance(MainActivity.this).delete(record); refreshList(null); } }); } } }); } @Bind(R.id.date) private void changeDateFilter(){ DatePickerDialog dialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mCalendar.set(year,monthOfYear,1); refreshDataView(); mAdapter.setFilterDate(mCalendar.getTimeInMillis()); } },mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } /** * 接收到数据列表刷新请求后开始刷新 * @param event 刷新请求事件 */ @Event private void refreshList(ERefreshList event){ mAdapter.refresh(); refreshDataView(); } private long getEndTime(){ Calendar calendar=Calendar.getInstance(); calendar.setTime(mCalendar.getTime()); if(calendar.get(Calendar.MONTH)>=11){ calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+1); calendar.set(Calendar.MONTH,0); } else calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1); return calendar.getTimeInMillis(); }}