Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package things.com;
- //新增記事頁
- import android.app.Activity;
- import android.os.Bundle;
- import android.provider.BaseColumns;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.Intent;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import android.view.View;
- import android.app.Dialog;
- import android.app.DatePickerDialog;
- import android.app.TimePickerDialog;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.TimePicker;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class things2 extends Activity {
- private TextView mDateDisplay;
- private Button mPickDate;
- private int mYear;
- private int mMonth;
- private int mDay;
- private TextView mTimeDisplay;
- private Button mPickTime;
- private int mHour;
- private int mMinute;
- static final int DATE_DIALOG_ID = 0;
- static final int TIME_DIALOG_ID = 1;
- final EditText mEditText01 = (EditText)findViewById(R.id.title_text);
- final EditText mEditText02 = (EditText)findViewById(R.id.things_text);
- DbHelper helper;
- public interface UserSchema {
- String DB_NAME = "Agendum"; //記事資料庫
- String C_ID = "_ID"; //編號
- String C_TITLE = "Agendum_title"; //標題
- String C_CONTENT = "Agendum_content"; //記事內容
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.things2);
- helper = new DbHelper(this);
- final SQLiteDatabase db = helper.getWritableDatabase();
- //save_btn
- Button save = (Button) findViewById(R.id.save_btn);
- save.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- ContentValues values = new ContentValues();
- values.put(UserSchema.C_TITLE, mEditText01.getText().toString());
- values.put(UserSchema.C_CONTENT, mEditText02.getText().toString());
- SQLiteDatabase db = helper.getWritableDatabase();
- db.insert(UserSchema.DB_NAME, null, values);
- db.close();
- onCreate(savedInstanceState);
- }
- });
- //back_btn
- Button next = (Button) findViewById(R.id.back_btn);
- next.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Intent intent = new Intent();
- setResult(RESULT_OK, intent);
- finish();
- }
- });
- //date_btn
- mDateDisplay = (Button) findViewById(R.id.date_btn);
- mPickDate = (Button) findViewById(R.id.date_btn);
- mPickDate.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- showDialog(DATE_DIALOG_ID);
- }
- });
- //time_btn
- mTimeDisplay = (Button) findViewById(R.id.time_btn);
- mPickTime = (Button) findViewById(R.id.time_btn);
- mPickTime.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- showDialog(TIME_DIALOG_ID);
- }
- });
- //clean_btn
- Button clean = (Button) findViewById(R.id.clean_btn);
- clean.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- mTimeDisplay.setText("-- : --");
- mDateDisplay.setText("-- / -- / --");
- }
- });
- }
- //date_btn-1
- private void update_date() {
- mDateDisplay.setText(
- new StringBuilder()
- .append(mYear).append("-")
- .append(mMonth + 1).append("-")
- .append(mDay));
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DATE_DIALOG_ID:
- return new DatePickerDialog(this,
- mDateSetListener,
- mYear, mMonth, mDay);
- //time_btn (case)
- case TIME_DIALOG_ID:
- return new TimePickerDialog(this,
- mTimeSetListener, mHour, mMinute, false);
- }
- return null;
- }
- private DatePickerDialog.OnDateSetListener mDateSetListener =
- new DatePickerDialog.OnDateSetListener() {
- public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
- mYear = year;
- mMonth = monthOfYear;
- mDay = dayOfMonth;
- update_date();
- }
- };
- //time_btn-1
- private void update_time() {
- mTimeDisplay.setText(
- new StringBuilder()
- .append(pad(mHour)).append(":")
- .append(pad(mMinute)));
- }
- private static String pad(int c) {
- if (c >= 10)
- return String.valueOf(c);
- else
- return "0" + String.valueOf(c);
- }
- private TimePickerDialog.OnTimeSetListener mTimeSetListener =
- new TimePickerDialog.OnTimeSetListener() {
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- mHour = hourOfDay;
- mMinute = minute;
- update_time();
- }
- };
- public static class DbHelper extends SQLiteOpenHelper{
- private static final String TAG =DbHelper.class.getSimpleName();
- public static final String DB_NAME = "Agendum.db";
- public static final int DB_VERSION = 1;
- public static final String TABLE = "Agendum";
- public static final String C_ID = BaseColumns._ID;
- public static final String C_CONTENT = "Agendum_content";
- public static final String C_TITLE = "Agendum_title";
- Context context;
- public DbHelper(Context context) {
- super(context, DB_NAME, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String sql = "CREATE TABLE " + DB_NAME + " ("
- + C_ID + " INTEGER primary key autoincrement, "
- + C_TITLE + " text not null, "
- + C_CONTENT + " text not null, ";
- //Log.i("haiyang:createDB=", sql);
- db.execSQL(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("drop table if exists " + TABLE);
- Log.d(TAG, "onUpdate dropped table " + TABLE);
- this.onCreate(db);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment