2014年8月1日 星期五

觀察: 開發筆記: 4) 資料庫程式 , 主程式

資料庫

本專案會用到資料庫來存放資料,因此需設計一個資料庫輔助類別,進行資料表的建立及異動更新使用。

專案 > New > Class > MyDBHelper

1. 繼承 extends SQLiteOpenHelper
2. 必須處理建構子。
3. 在 onCreate()呼叫 SQLiteDatabase 物件的 execSQL() 進行資料表建立。
4. 在 onUpgrade() 呼叫 SQLiteDatabase 物件的 execSQL() 刪除員資料表,並呼叫 onCreate() 建立新版本的資料表。

MyDBHelper.java
  1. package tw.brad.android.apps.MyNotebook;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5. import android.database.sqlite.SQLiteOpenHelper;  
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  7.   
  8. public class MyDBHelper extends SQLiteOpenHelper {  
  9.     private final String createTableSQL =   
  10.         "CREATE TABLE IF NOT EXISTS notebook " +  
  11.         "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +   
  12.         "title TEXT, " +   
  13.         "priority INTEGER, " +   
  14.         "content TEXT, " +   
  15.         "del TEXT, " +   
  16.         "status TEXT)";  
  17.     public MyDBHelper(Context context, String name, CursorFactory factory,  
  18.             int version) {  
  19.         super(context, name, factory, version);  
  20.     }  
  21.   
  22.     @Override  
  23.     public void onCreate(SQLiteDatabase db) {  
  24.         db.execSQL(createTableSQL);  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  29.         db.execSQL("DROP TABLE IF EXISTS notebook");  
  30.         onCreate(db);  
  31.     }  
  32.   
  33. }  
參考資料:http://ithelp.ithome.com.tw/question/10139113
MyNotebook 主畫面程式

1. 處理新增筆記資料的按鈕事件。
 a. 建立 MyDBHelper 物件
 b. 取出組態設定的預設值
 c. 執行 SQL 新增一筆筆記資料
 d. 建立 intent 發出特定 Broadcast 給筆記資料的 Activity 使用

2. 處理頁籤之間的轉換。
 a. 取得系統資源物件
 b. 設定個別的頁籤相關參數資料
 c. 設定個別頁籤的 Intent

MyNotebook.java
  1. package tw.brad.android.apps.MyNotebook;  
  2.   
  3.   
  4. import android.app.TabActivity;  
  5. import android.content.Intent;  
  6. import android.content.SharedPreferences;  
  7. import android.content.res.Resources;  
  8. import android.database.sqlite.SQLiteDatabase;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.EditText;  
  13. import android.widget.ImageButton;  
  14. import android.widget.TabHost;  
  15.   
  16. public class MyNotebook extends TabActivity {  
  17.     private ImageButton add;  
  18.     private EditText newnote;     
  19.     private MyDBHelper dbHelper;  
  20.     private SQLiteDatabase db;  
  21.     private static SharedPreferences sdata;  
  22.       
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         main();  
  27.           
  28.     }  
  29.       
  30.     private void main(){  
  31.         setContentView(R.layout.main);  
  32.   
  33.         add = (ImageButton)findViewById(R.id.add);  
  34.         newnote = (EditText)findViewById(R.id.newnote);  
  35.           
  36.         add.setOnClickListener(new OnClickListener(){  
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 dbHelper = new MyDBHelper(MyNotebook.this"brad", null, 1);  
  40.                 db = dbHelper.getReadableDatabase();  
  41.                   
  42.                 sdata = getSharedPreferences("Setting", MODE_PRIVATE);         
  43.                 int priority = sdata.getInt("priority", 2) +1;    
  44.                   
  45.                 String title = newnote.getText().toString().trim();  
  46.                 String status = "yet";  
  47.                   
  48.                 db.execSQL(  
  49.                         "INSERT INTO notebook " +  
  50.                         "(title,priority,del,status) VALUES " +   
  51.                         "('" +title+ "'," +   
  52.                             priority + ",'false', '" +   
  53.                             status + "')");  
  54.                 Intent intent = new Intent("tw.brad.android.apps.MyNotebook");  
  55.                 sendBroadcast(intent);  
  56.                 newnote.setText("");  
  57.             }  
  58.         });  
  59.   
  60.   
  61.         Resources res = getResources();   
  62.         TabHost tabHost = getTabHost();  
  63.           
  64.         TabHost.TabSpec spec;  
  65.         Intent intent;  
  66.   
  67.         intent = new Intent().setClass(this, ToDo.class);  
  68.         spec = tabHost.newTabSpec(res.getString(R.string.notebook))  
  69.                         .setIndicator(res.getString(R.string.notebook),  
  70.                           res.getDrawable(R.drawable.todo))  
  71.                           .setContent(intent);  
  72.         tabHost.addTab(spec);  
  73.   
  74.         intent = new Intent().setClass(this, Done.class);  
  75.         spec = tabHost.newTabSpec(res.getString(R.string.garbage))  
  76.                         .setIndicator(res.getString(R.string.garbage),  
  77.                           res.getDrawable(R.drawable.done))  
  78.                           .setContent(intent);  
  79.         tabHost.addTab(spec);  
  80.   
  81.         intent = new Intent().setClass(this, Config.class);  
  82.         spec = tabHost.newTabSpec(res.getString(R.string.setting))  
  83.                         .setIndicator(res.getString(R.string.setting),  
  84.                           res.getDrawable(R.drawable.config))  
  85.                           .setContent(intent);  
  86.         tabHost.addTab(spec);  
  87.           
  88.         tabHost.setCurrentTab(0);  
  89.           
  90.     }  
  91.       
  92. }  
參考資料:http://ithelp.ithome.com.tw/question/10139337

沒有留言:

張貼留言