From 7932253a30a19c924a5f2fdae6ad904de76f1582 Mon Sep 17 00:00:00 2001 From: lwj000 <602078721@qq.com> Date: Tue, 24 May 2016 15:53:10 +0800 Subject: [PATCH] =?UTF-8?q?#92#27=E5=AE=9E=E9=AA=8C6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改了主界面视图,增加了插入信息界面,使用了SQliteDatabase数据库进行数据存储 --- .../Com1314080901221Activity_Insert.java | 52 +++++++++++++++++++ .../Com1314080901221_MyDBHelper.java | 35 +++++++++++++ .../activity_com1314080901221_insert.xml | 47 +++++++++++++++++ .../layout/activity_com1314080901221_main.xml | 20 +++---- .../activity_com1314080901221_main_0.xml | 12 +++++ .../activity_com1314080901221_mian_drawer.xml | 15 ++++++ .../activity_com1314080901221_toolbar.xml | 11 ++++ .../main/res/menu/com1314080901221_main.xml | 21 ++++++++ 8 files changed, 204 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221Activity_Insert.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221_MyDBHelper.java create mode 100644 app/src/main/res/layout/activity_com1314080901221_insert.xml create mode 100644 app/src/main/res/layout/activity_com1314080901221_main_0.xml create mode 100644 app/src/main/res/layout/activity_com1314080901221_mian_drawer.xml create mode 100644 app/src/main/res/layout/activity_com1314080901221_toolbar.xml create mode 100644 app/src/main/res/menu/com1314080901221_main.xml diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221Activity_Insert.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221Activity_Insert.java new file mode 100644 index 00000000..d6928cb5 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221Activity_Insert.java @@ -0,0 +1,52 @@ +package com.example.lwj_pc.my_classwork; + +import android.app.Activity; +import android.content.ContentValues; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by LWJ-PC on 2016/5/10. + */ +public class Com1314080901221Activity_Insert extends Activity { + private EditText title,content; + private Button tijiao; + private Com1314080901221_MyDBHelper dbHelper; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_com1314080901221_insert); + + dbHelper=new Com1314080901221_MyDBHelper(this,"Myapp.db",null,1); //获取数据库实例 + + title=(EditText)findViewById(R.id.insert_title); + content=(EditText)findViewById(R.id.insert_context); + tijiao=(Button)findViewById(R.id.insert_tijiao); + tijiao.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String message_title = title.getText().toString(); + String message_content = content.getText().toString(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd;HH:mm:ss"); + Date date=new Date(); + String datetime = dateFormat.format(date); + Toast.makeText(Com1314080901221Activity_Insert.this, datetime, Toast.LENGTH_SHORT).show(); + + SQLiteDatabase db=dbHelper.getWritableDatabase(); + ContentValues values=new ContentValues(); + values.put("title",message_title); + values.put("content",message_content); + values.put("time",datetime); + db.insert("journey",null,values); + } + }); + + } +} + diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221_MyDBHelper.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221_MyDBHelper.java new file mode 100644 index 00000000..7ce25611 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901221/Com1314080901221_MyDBHelper.java @@ -0,0 +1,35 @@ +package com.example.lwj_pc.my_classwork; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.widget.Toast; + +/** + * Created by LWJ-PC on 2016/5/10. + */ +public class Com1314080901221_MyDBHelper extends SQLiteOpenHelper { + //定义一个Context变量,用于保存活动调用本参数时传入的Context,并方便本类的其他方法使用。 + private Context mycontext; + //此处书写数据库建表语句 + public static final String CREATE_TABLE="create table journey (" + +"title text," + +"content text," + +"time text)"; + //构造函数 + public Com1314080901221_MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { + super(context, name, factory, version); + mycontext=context; + } + //创建时执行,当检查到数据库不存在时,执行onCreate进行创建。 + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(CREATE_TABLE); + Toast.makeText(mycontext, "建表成功", Toast.LENGTH_SHORT).show(); + } + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } + +} diff --git a/app/src/main/res/layout/activity_com1314080901221_insert.xml b/app/src/main/res/layout/activity_com1314080901221_insert.xml new file mode 100644 index 00000000..0ba3aeb6 --- /dev/null +++ b/app/src/main/res/layout/activity_com1314080901221_insert.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + +