public class ToDoDB extends SQLiteOpenHelper
{
private final static String DATABASE_NAME = "todo_db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "todo_table";
public final static String FIELD_id = "_id";
public final static String FIELD_TEXT = "todo_text";
public final static String FIELD_SPINNER1 = "todo_spinner1";
public final static String FIELD_SPINNER2 = "todo_spinner2";
public final static String FIELD_SPINNER3 = "todo_spinner3";
public ToDoDB(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public long insert(String text)
{
SQLiteDatabase db = this.getWritableDatabase();
/* 將新增的值放入ContentValues */
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
long row = db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id, String text)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
/* 將修改的值放入ContentValues */
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
private int _id;
private Bundle savedInstanceState;
protected final static int MENU_ADD = Menu.FIRST;
protected final static int MENU_EDIT = Menu.FIRST + 1;
protected final static int MENU_DELETE = Menu.FIRST + 2;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item); //提供選擇類別
switch (item.getItemId())
{
case MENU_ADD: //新增資料事件
this.addTodo();
break;
case MENU_EDIT: //編輯資料事件
this.editTodo();
break;
case MENU_DELETE: //刪除資料事件
this.deleteTodo();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);