统计界面RecordFragment中的业务逻辑涉及到三个方面:新建任务,每日签到和删除任务。有上面三个中的任何一个操作时,统计界面都要做出相应的更新。其中删除操作在统计界面中进行。
这里参考了文章中实现自定义ListView的形式。
TaskBean类在上一篇文章中已经介绍过了,下面定义一个接口
ITaskFinder:
public interface ITaskFinder {
List<TaskBean> findAllTask();
}
接口中只有一个方法
findAllTask,获取所有任务的列表。
接口实现类
ITaskFinderImpl如下:
@EBean
public class ITaskFinderImpl implements ITaskFinder {
@RootContext
Context context;
private MyDatabaseHelper dbHelper = null;
@Override
public List<TaskBean> findAllTask() {
if ( dbHelper == null) {
dbHelper = new MyDatabaseHelper( context, "di.db3", 1);
}
return DailyInsistDao. getTasks(dbHelper, 0, -1);
}
}
因为RecordFragment要列出所有的任务并统计它们的信息,所以我们需要一个
TaskItemAdapter来绑定数据到视图上,
TaskItemView是列表项的视图,如下:
@EViewGroup(R.layout.task_item )
public class TaskItemView extends LinearLayout {
@ViewById
TextView tv_rate, tv_item_title, tv_start_to_end;
@ViewById
LinearLayout ll_rec_rate, ll_main;
@StringRes
String rate;
@DrawableRes
Drawable orange_rec, blue_rec, gray_rec, btn_up, btn_down;
@ViewById
Button btn_toggle;
/*
* set toggle button to control show or hide delete part
*/
@ViewById
RelativeLayout rl_del;
/*
* for delete
*/
private int taskId;
public TaskItemView(Context context) {
super(context);
}
public void bind(TaskBean task) {
taskId = task.getTaskId();
/*
* show rate view
*/
tv_rate.setText(String. format(rate, task.getCompleted(),
task.getDuring()));
/*
* show title view
*/
tv_item_title.setText(task.getContent());
/*
* show rectangle rate
*/
ll_rec_rate.removeAllViews();
/*
* the task is finished
*/
if (task.getCompleted() == task.getDuring()) {
for ( int i = 0, count = task.getDuring(); i < count; i++) {
ll_rec_rate.addView(getRegBackgroundByDrawable( ll_rec_rate,
orange_rec));
}
} else {
/*
* if the task is not finished, completed parts use blue rectangle
* and res