標籤:
該篇說明
該篇我們來詳解建立計劃功能實現。
建立計劃功能是在建立計劃介面中的。
也就是在CreatePlanActivity中
在後台模組中只描述管理層,其它的資料庫操作等請自行查看
效果
執行流程圖
建立計劃UI介面功能說明
1> 橫幅顯示建立計劃字元
hfTitleTextView = (TextView) findViewById(R.id.hfTitleTextView);hfTitleTextView.setText("建立計劃");
2> 建立計劃項的填寫
a步驟> 單詞書名輸入操作,只需要顯示組件和擷取組件即可
private void init() { studyNameEditText = (EditText) findViewById(R.id.studyNameEditText);}
b步驟1> 初始化選擇單詞書相關屬性及值
private void init() {
selectWordBookRelativeLayout = (RelativeLayout) findViewById(R.id.selectWordBookRelativeLayout); wordBooknameTextView = (TextView) findViewById(R.id.wordBooknameTextView); wordBookname = WordBookConstant.WDBK_CATE_ARRAY[0]; wordBooknameTextView.setText(wordBookname);
}
b步驟2> 選擇單詞書布局點擊監聽
private void logic() { selectWordBookRelativeLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { alertSelectWorkBook(); } });}
b步驟3> 彈出的單選框選擇單詞書
private void alertSelectWorkBook () { new AlertDialog.Builder(context) .setTitle("請選擇書籍") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(WordBookConstant.WDBK_CATE_ARRAY, wordBookSelectedIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
wordBookname = WordBookConstant.WDBK_CATE_ARRAY[which]; wordBookSelectedIndex = which; wordBooknameTextView.setText(wordBookname); Toast.makeText(context, "單詞書是:" + wordBookname, Toast.LENGTH_LONG).show(); } } ) .setNegativeButton("取消", null) .show();}
c步驟1> 初始選擇每天單元量布局組件及值
private void init() { selectDayUnitRelativeLayout = (RelativeLayout) findViewById(R.id.selectDayUnitRelativeLayout); dayUnitTextView = (TextView) findViewById(R.id.dayUnitTextView); dayUnitStr = ReciteConstant.DUNITCT_ITEMS_ARRAY[0]; dayUnitTextView.setText(dayUnitStr);}
c步驟2> 選擇每天單詞量布局點擊監聽
private void logic() { selectDayUnitRelativeLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { alertSelectDayUnit(); } });}
c步驟3> 彈出的單選框選擇每天單元量
private void alertSelectDayUnit() { new AlertDialog.Builder(context) .setTitle("請選擇每天單元量") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(ReciteConstant.DUNITCT_ITEMS_ARRAY, dayUnitSelectedIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); dayUnitStr = ReciteConstant.DUNITCT_ITEMS_ARRAY[which]; dayUnitSelectedIndex = which; dayUnitTextView.setText(dayUnitStr); Toast.makeText(context, "每天單元量:" + dayUnitStr, Toast.LENGTH_LONG).show(); } } ) .setNegativeButton("取消", null) .show(); }
3> 建立學習計劃確定按鈕監聽
private void logic() { confirmButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if(validate()) { createStudyPlan(); } } });}
4> 效驗資訊
在建立前需要進效驗資訊,也就是輸入和選擇項的資訊的
這些資訊將作為計劃的一些屬性
private boolean validate() { studyPlaname = studyNameEditText.getText().toString().trim(); if(studyPlaname == null || "".equals(studyPlaname)) { Toast.makeText(context, "學習名必須填寫", Toast.LENGTH_LONG).show(); return false; } return true;}
5> 建立學習計劃方法
因為是耗時操作,所以也需要在多線程完成
private void createStudyPlan() { createStudyPlanProgressDialog.setMessage("正在建立計劃資料"); createStudyPlanProgressDialog.show(); Thread createStudyPlanThread = new Thread(new CreateStudyPlanThread()); createStudyPlanThread.start();}
6> 建立學習計劃線程
除了建立計劃以外,還需要初始單詞表資料,也就是將sdcard對應的單詞書資料複製到了學習計劃中
class CreateStudyPlanThread implements Runnable { public void run() { //建立計劃 new StudyPlanManager().create( studyPlaname, wordBookname, Integer.parseInt(dayUnitStr)); //初始單詞表資料 new WordManager().initData(studyPlaname, wordBookname);
SystemUtil.sleepvd(2000); myHandler.sendEmptyMessage(0); } }
建立計劃功能後台模組講解
建立計劃功能調用了兩個管理模組
一個是計劃管理模組StudyPlanManager
一個單詞管理模組WordManager
1> 計劃管理的模組建立功能
也就是建立計劃功能
public void create(String studyPlaname, String wordBookname, int dayUnit) { if(studyPlanDao == null) { studyPlanDao = new StudyPlanDao(); } //--------- // 建立學習計劃對象 StudyPlan studyPlan = new StudyPlan(); studyPlan.name = studyPlaname; studyPlan.wordBookname = wordBookname; studyPlan.allWordCount = WordBookConstant.getWordCountFromWordBook(wordBookname); studyPlan.dayUnitCount = dayUnit; //總單元量(除不盡+1,最後一個單元少於70) studyPlan.allUnitCount = studyPlan.allWordCount % ReciteConstant.DUNITG_WORD_COUNT == 0 ? studyPlan.allWordCount / ReciteConstant.DUNITG_WORD_COUNT : studyPlan.allWordCount / ReciteConstant.DUNITG_WORD_COUNT + 1; //所有新單元耗時天數,不算複習(除不盡+1, 最後一天少於70個單詞) studyPlan.needDayCount = studyPlan.allWordCount % (dayUnit * ReciteConstant.DUNITG_WORD_COUNT) == 0 ? studyPlan.allWordCount / (dayUnit * ReciteConstant.DUNITG_WORD_COUNT) : studyPlan.allWordCount / (dayUnit * ReciteConstant.DUNITG_WORD_COUNT) + 1; studyPlan.createDated = DateUtil.getDateString(DateUtil.FORMATE_DAY); //一些計劃中背誦的屬性 studyPlan.lastTodayTaskState = ReciteConstant.TODAYTASK_STUDY; Global.defaultReciteTimes = Integer.parseInt(ReciteConstant.RECTIMES_ITEMS_ARRAY[0]); studyPlan.lastReciteProgressStr = 0 + "/" + Global.defaultReciteTimes; studyPlan.lastUnitGroupIndex = 0; //--------- //--------- //添加資料 String spPathStr = PathConstant.STUPLAN_PATH + "sp_" + studyPlaname; // 建立學習計劃檔案 createStudyPlanFile(spPathStr); //添加資料到學習計劃資料庫中 studyPlanDao.add(studyPlan, spPathStr); //---------}
2> 單詞管理的的初始單詞表資料
就是將sdcard對應的單詞書資料複製到了學習計劃中
調用了單詞書的資料庫操作以及單詞的資料庫操作,這裡資料庫操作都是本地的sqlite3
public void initData(String studyPlaname, String wordBookname) { if(wordDao == null) { wordDao = new WordDao(); } if(wordBookDao == null) { wordBookDao = new WordBookDao(); } List<String> wordStrList = wordBookDao.findWordBook(wordBookname); String pathStr = PathConstant.STUPLAN_PATH + "sp_" + studyPlaname; wordDao.initAddMoreUseTransaction(wordStrList, pathStr);
}
更多請自行下載源碼解讀
應用下載:請點擊
源碼下載:請點擊
android項目實戰-背唄單詞DEV02-建立計劃實現