本文实例为大家分享了android实现个人信息管理系统的具体代码,供大家参考,具体内容如下
要求:使用sqlite实现个人信息管理系统,个人信息包括姓名,年龄,性别以及学历
(1)dbhelper.java代码如下:
package com.example.system; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class dbhelper extends sqliteopenhelper{ private static final string db_name ="people.db"; private static final string tbl_name="message"; private sqlitedatabase db; public dbhelper(context c){ super(c,db_name,null,2); } @override public void oncreate(sqlitedatabase db){ this.db=db; string create_tbl="create table message(_id integer primary key autoincrement,name text,age text,sex text,edu text)"; db.execsql(create_tbl); } public void insert(contentvalues values){ sqlitedatabase db=getwritabledatabase(); db.insert(tbl_name, null, values); db.close(); } public cursor query(){ sqlitedatabase db=getwritabledatabase(); cursor c=db.query(tbl_name, null, null, null, null, null, null); return c; } @override public void onupgrade(sqlitedatabase arg0, int arg1, int arg2) { // todo auto-generated method stub } }
(2)mainactivity.java代码如下:
package com.example.system; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class mainactivity extends activity { private textview textview; private button btn1,btn2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview = (textview)findviewbyid(r.id.textview); btn1 = (button)findviewbyid(r.id.btn1); btn2 = (button)findviewbyid(r.id.btn2); //添加监听器 btn1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(mainactivity.this,addactivity.class); startactivity(intent); } }); btn2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(mainactivity.this,queryactivity.class); startactivity(intent); } }); } }
对应布局文件如下:
(3)addactivity.java代码如下:
package com.example.system; import android.app.activity; import android.content.contentvalues; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class addactivity extends activity { //声明组件 private edittext name,age,sex,edu; private button add; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add); name=(edittext)findviewbyid(r.id.name); age=(edittext)findviewbyid(r.id.age); sex=(edittext)findviewbyid(r.id.sex); edu=(edittext)findviewbyid(r.id.edu); add=(button)findviewbyid(r.id.buttonadd); //根据id 获取组件 add.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string name1=name.gettext().tostring(); string age1=age.gettext().tostring(); string sex1=sex.gettext().tostring(); string edu1=edu.gettext().tostring(); //封装信息 contentvalues values=new contentvalues(); values.put("name", name1); values.put("age", age1); values.put("sex", sex1); values.put("edu", edu1); dbhelper helper=new dbhelper(getapplicationcontext()); helper.insert(values); intent intent = new intent(addactivity.this,mainactivity.class); startactivity(intent); } }); } }
对应布局文件如下:
(4)queryactivity.java代码如下:
package com.example.system; import android.app.alertdialog; import android.app.listactivity; import android.content.dialoginterface; import android.database.cursor; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import android.widget.simplecursoradapter; public class queryactivity extends listactivity{ private listview listview=null; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); this.settitle("浏览信息"); listview=getlistview(); final dbhelper helper=new dbhelper(this); cursor c=helper.query(); string[] from={"_id","name","age","sex","edu"}; int[] to={r.id.text0,r.id.text1,r.id.text2,r.id.text3,r.id.text4}; simplecursoradapter adapter=new simplecursoradapter(this, r.layout.activity_query, c, from, to); listview.setadapter(adapter); } }
对应布局文件如下:
实现效果如下: