Guest User

Untitled

a guest
Nov 13th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. package things.com;
  2. //新增記事頁
  3.  
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.provider.BaseColumns;
  7. import android.content.ContentValues;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteOpenHelper;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.app.Dialog;
  15. import android.app.DatePickerDialog;
  16. import android.app.TimePickerDialog;
  17. import android.widget.DatePicker;
  18. import android.widget.EditText;
  19. import android.widget.TimePicker;
  20. import android.view.View.OnClickListener;
  21. import android.widget.Button;
  22. import android.widget.TextView;
  23.  
  24.  
  25. public class things2 extends Activity {
  26. private TextView mDateDisplay;
  27. private Button mPickDate;
  28. private int mYear;
  29. private int mMonth;
  30. private int mDay;
  31. private TextView mTimeDisplay;
  32. private Button mPickTime;
  33. private int mHour;
  34. private int mMinute;
  35.  
  36. static final int DATE_DIALOG_ID = 0;
  37. static final int TIME_DIALOG_ID = 1;
  38.  
  39. final EditText mEditText01 = (EditText)findViewById(R.id.title_text);
  40. final EditText mEditText02 = (EditText)findViewById(R.id.things_text);
  41. DbHelper helper;
  42. public interface UserSchema {
  43. String DB_NAME = "Agendum"; //記事資料庫
  44. String C_ID = "_ID"; //編號
  45.  
  46. String C_TITLE = "Agendum_title"; //標題
  47. String C_CONTENT = "Agendum_content"; //記事內容
  48. }
  49.  
  50. /** Called when the activity is first created. */
  51. @Override
  52. public void onCreate(final Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.things2);
  55.  
  56. helper = new DbHelper(this);
  57. final SQLiteDatabase db = helper.getWritableDatabase();
  58.  
  59. //save_btn
  60. Button save = (Button) findViewById(R.id.save_btn);
  61. save.setOnClickListener(new View.OnClickListener() {
  62. public void onClick(View view) {
  63. ContentValues values = new ContentValues();
  64. values.put(UserSchema.C_TITLE, mEditText01.getText().toString());
  65. values.put(UserSchema.C_CONTENT, mEditText02.getText().toString());
  66. SQLiteDatabase db = helper.getWritableDatabase();
  67. db.insert(UserSchema.DB_NAME, null, values);
  68. db.close();
  69. onCreate(savedInstanceState);
  70. }
  71. });
  72.  
  73. //back_btn
  74. Button next = (Button) findViewById(R.id.back_btn);
  75. next.setOnClickListener(new View.OnClickListener() {
  76. public void onClick(View view) {
  77. Intent intent = new Intent();
  78. setResult(RESULT_OK, intent);
  79. finish();
  80. }
  81. });
  82.  
  83. //date_btn
  84. mDateDisplay = (Button) findViewById(R.id.date_btn);
  85. mPickDate = (Button) findViewById(R.id.date_btn);
  86. mPickDate.setOnClickListener(new View.OnClickListener() {
  87. public void onClick(View v) {
  88. showDialog(DATE_DIALOG_ID);
  89. }
  90. });
  91. //time_btn
  92. mTimeDisplay = (Button) findViewById(R.id.time_btn);
  93. mPickTime = (Button) findViewById(R.id.time_btn);
  94. mPickTime.setOnClickListener(new View.OnClickListener() {
  95. public void onClick(View v) {
  96. showDialog(TIME_DIALOG_ID);
  97. }
  98. });
  99. //clean_btn
  100. Button clean = (Button) findViewById(R.id.clean_btn);
  101. clean.setOnClickListener(new View.OnClickListener() {
  102. public void onClick(View view) {
  103. mTimeDisplay.setText("-- : --");
  104. mDateDisplay.setText("-- / -- / --");
  105. }
  106. });
  107.  
  108. }
  109. //date_btn-1
  110. private void update_date() {
  111. mDateDisplay.setText(
  112. new StringBuilder()
  113. .append(mYear).append("-")
  114. .append(mMonth + 1).append("-")
  115. .append(mDay));
  116. }
  117. @Override
  118. protected Dialog onCreateDialog(int id) {
  119. switch (id) {
  120. case DATE_DIALOG_ID:
  121. return new DatePickerDialog(this,
  122. mDateSetListener,
  123. mYear, mMonth, mDay);
  124. //time_btn (case)
  125. case TIME_DIALOG_ID:
  126. return new TimePickerDialog(this,
  127. mTimeSetListener, mHour, mMinute, false);
  128. }
  129. return null;
  130. }
  131. private DatePickerDialog.OnDateSetListener mDateSetListener =
  132. new DatePickerDialog.OnDateSetListener() {
  133. public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
  134. mYear = year;
  135. mMonth = monthOfYear;
  136. mDay = dayOfMonth;
  137. update_date();
  138. }
  139. };
  140.  
  141. //time_btn-1
  142. private void update_time() {
  143. mTimeDisplay.setText(
  144. new StringBuilder()
  145. .append(pad(mHour)).append(":")
  146. .append(pad(mMinute)));
  147. }
  148. private static String pad(int c) {
  149. if (c >= 10)
  150. return String.valueOf(c);
  151. else
  152. return "0" + String.valueOf(c);
  153. }
  154.  
  155. private TimePickerDialog.OnTimeSetListener mTimeSetListener =
  156. new TimePickerDialog.OnTimeSetListener() {
  157. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  158. mHour = hourOfDay;
  159. mMinute = minute;
  160. update_time();
  161. }
  162. };
  163. public static class DbHelper extends SQLiteOpenHelper{
  164. private static final String TAG =DbHelper.class.getSimpleName();
  165.  
  166. public static final String DB_NAME = "Agendum.db";
  167. public static final int DB_VERSION = 1;
  168. public static final String TABLE = "Agendum";
  169. public static final String C_ID = BaseColumns._ID;
  170. public static final String C_CONTENT = "Agendum_content";
  171. public static final String C_TITLE = "Agendum_title";
  172.  
  173. Context context;
  174.  
  175. public DbHelper(Context context) {
  176. super(context, DB_NAME, null, DB_VERSION);
  177. }
  178.  
  179. @Override
  180. public void onCreate(SQLiteDatabase db) {
  181. String sql = "CREATE TABLE " + DB_NAME + " ("
  182. + C_ID + " INTEGER primary key autoincrement, "
  183. + C_TITLE + " text not null, "
  184. + C_CONTENT + " text not null, ";
  185. //Log.i("haiyang:createDB=", sql);
  186. db.execSQL(sql);
  187. }
  188.  
  189. @Override
  190. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  191. db.execSQL("drop table if exists " + TABLE);
  192. Log.d(TAG, "onUpdate dropped table " + TABLE);
  193. this.onCreate(db);
  194.  
  195. }
  196.  
  197. }
  198.  
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment