数据库的创建
package com.example.myapp;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.Widget.Toast;public class DbHelper extends sqliteOpenHelper { final String create_table="CREATE table Student (_ID integer primary key autoincrement,xm text,xh text,bj text,zy text,cj text)"; final String create_register="CREATE table Register (_ID integer primary key autoincrement,xm text,xh text)";//创建两张表,一张student,一张register表 Context context; public DbHelper(Context context,String dbname,int version){ super(context,dbname,null,version); this.context=context;//上下文 } @OverrIDe public voID onCreate(sqliteDatabase db) {db.execsql(create_table); db.execsql(create_register);} @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { db.execsql("drop table if exists Student"); db.execsql("drop table if exists Register"); db.execsql(create_table); db.execsql(create_register);//完成创建 }// 对表进行数据的插入方法 public voID insert(String tablename, ContentValues values){ sqliteDatabase db = getReadableDatabase(); db.insert(tablename,null,values); Toast.makeText(context,"成功插入数据!",Toast.LENGTH_SHORT).show(); }//查询表中所有 public Cursor queryAll(String tablename){ sqliteDatabase db = getReadableDatabase(); Cursor cursor = db.query(tablename,null,null,null,null,null,null); return cursor; }//对表中的姓名和学号进行单独的查询 public Boolean queryByStudentXhAndXm(String tablename,String xm,String xh){ sqliteDatabase db = getReadableDatabase(); Cursor cursor = db.query(tablename,new String[]{"xm,xh"},"xm=? and xh=?",new String[]{xm,xh},null,null,null); if (cursor.movetoFirst()) { return true; }else { return false; } }//删除表中数据的方法 public voID delStudent(String ID){ sqliteDatabase db = getWritableDatabase(); db.delete("Student","_ID=?",new String[]{ID}); }//对表进行更新 public voID updateStudent(String ID,ContentValues values){ sqliteDatabase db = getWritableDatabase(); db.update("Student",values,"_ID=?",new String[]{ID}); }}
创建了两张表格,一张用来储存学生信息,Student表:ID(主键),姓名,学号,班级,专业,成绩。
一张register表:ID(主键),姓名,学号。用来储存登录信息
其余类和其布局文件
MainActivity.class(登录界面)
package com.example.myapp;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { EditText et1,et2; button btn1,btn2; DbHelper dbHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dbHelper=new DbHelper(MainActivity.this,"MyDataBase,",3); setContentVIEw(R.layout.activity_main); et1=findVIEwByID(R.ID.et1); et2=findVIEwByID(R.ID.et2); btn1=findVIEwByID(R.ID.dl); btn2=findVIEwByID(R.ID.zc); btn1.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String xm=et1.getText().toString(); String xh=et2.getText().toString(); if (xm.isEmpty()||xh.isEmpty()){ Toast.makeText(MainActivity.this,"姓名或者学号不可为空",Toast.LENGTH_SHORT).show(); } if (dbHelper.queryByStudentXhAndXm("Register",xm,xh)){ Intent intent=new Intent(MainActivity.this,Manage.class); startActivity(intent); } } }); btn2.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent=new Intent(MainActivity.this,Register.class); startActivity(intent); } }); }}
activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et1" androID:hint="输入姓名"/> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et2" androID:hint="学号"/> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/dl" androID:text="登录"/> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="注册" androID:ID="@+ID/zc"/></linearLayout>
Register.class(注册界面)
package com.example.myapp;import androIDx.appcompat.app.AppCompatActivity;import androID.content.ContentValues;import androID.content.Intent;import androID.database.Cursor;import androID.os.Bundle;import androID.text.TextUtils;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class Register extends AppCompatActivity { EditText et1,et2; button btn1,btn2; DbHelper dbHelper; TextVIEw show; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_register); dbHelper=new DbHelper(Register.this,"MyDataBase,",3); et1=findVIEwByID(R.ID.xm_zc); et2=findVIEwByID(R.ID.xh_zc); show=findVIEwByID(R.ID.tv_show); btn1=findVIEwByID(R.ID.qd_zc); btn2=findVIEwByID(R.ID.fh); btn1.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { String xm=et1.getText().toString(); String xh=et2.getText().toString(); if(TextUtils.isEmpty(xm)||TextUtils.isEmpty(xh)){ Toast.makeText(Register.this,"姓名或者学号不能为空",Toast.LENGTH_SHORT).show(); return; } ContentValues values = new ContentValues(); values.put("xm",xm); values.put("xh",xh); dbHelper.insert("Register",values); showUser(); } }); btn2.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent=new Intent(Register.this,MainActivity.class); startActivity(intent); } }); } public voID showUser(){ Cursor cursor = dbHelper.queryAll("Register"); String str = "_ID xm xh\n"; if (cursor.movetoFirst()) while (cursor.movetoNext()){ str += cursor.getString(0)+" "; str += cursor.getString(1)+" "; str += cursor.getString(2)+"\n"; }; show.setText(str); }}
activity_register.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".Register"> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/xm_zc" androID:hint="输入姓名"/> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/xh_zc" androID:hint="输入学号"/> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="确认注册" androID:ID="@+ID/qd_zc"/> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="返回上一层" androID:ID="@+ID/fh"/> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:ID="@+ID/tv_show"/></linearLayout>
Manage.class(管理界面)
package com.example.myapp;import androIDx.appcompat.app.AlertDialog;import androIDx.appcompat.app.AppCompatActivity;import androID.content.ContentValues;import androID.content.DialogInterface;import androID.content.Intent;import androID.database.Cursor;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.SimpleCursorAdapter;public class Manage extends AppCompatActivity { ListVIEw ListVIEw; button btn; DbHelper dbHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_manage); AlertDialog.Builder builder = new AlertDialog.Builder(Manage.this); dbHelper = new DbHelper(Manage.this,"MyDataBase,",3); ListVIEw=findVIEwByID(R.ID.List); dbHelper.getWritableDatabase(); renderListVIEw(); btn=findVIEwByID(R.ID.new_List); btn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent=new Intent(Manage.this,NewStudent.class); startActivityForResult(intent,1); } }); ListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long IDi) { Cursor cursor = dbHelper.queryAll("Student"); cursor.move(position+1); String ID = cursor.getString(cursor.getColumnIndex("_ID")); String xm = cursor.getString(cursor.getColumnIndex("xm")); String xh = cursor.getString(cursor.getColumnIndex("xh")); String bj = cursor.getString(cursor.getColumnIndex("bj")); String zy = cursor.getString(cursor.getColumnIndex("zy")); String cj = cursor.getString(cursor.getColumnIndex("cj")); Intent intent = new Intent(Manage.this,NewStudent.class); intent.putExtra("ID",ID); intent.putExtra("xm",xm); intent.putExtra("xh",xh); intent.putExtra("bj",bj); intent.putExtra("zy",zy); intent.putExtra("cj",cj); startActivityForResult(intent,2); } }); ListVIEw.setonItemLongClickListener(new AdapterVIEw.OnItemLongClickListener() { @OverrIDe public boolean onItemLongClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long IDi) { Cursor cursor = dbHelper.queryAll("Student"); cursor.move(position+1); String ID = cursor.getString(cursor.getColumnIndex("_ID"));//getColumnIndex("_ID")得到这一列 String xm = cursor.getString(cursor.getColumnIndex("xm")); builder.setTitle("删除确认").setMessage("是否确认删除学生"+xm); builder.setPositivebutton("确定", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dbHelper.delStudent(ID); renderListVIEw(); } }); builder.show(); return true; } }); } private voID renderListVIEw() { Cursor cursor = dbHelper.queryAll("Student"); String from[] = new String[]{"_ID", "xm", "xh","bj","zy","cj"}; int to[] = new int[]{R.ID.ID,R.ID.xm, R.ID.xh, R.ID.bj, R.ID.zy, R.ID.cj}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.ListvIEw, cursor, from, to, 0); ListVIEw ListVIEw = findVIEwByID(R.ID.List); ListVIEw.setAdapter(adapter); } @OverrIDe protected voID onActivityResult(int reqCode,int resultCode,Intent intent){ super.onActivityResult(reqCode,resultCode,intent); if (resultCode==RESulT_OK){ String xm = intent.getStringExtra("xm"); String xh = intent.getStringExtra("xh"); String bj = intent.getStringExtra("bj"); String zy = intent.getStringExtra("zy"); String cj = intent.getStringExtra("cj"); dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("xm",xm); values.put("xh",xh); values.put("bj",bj); values.put("zy",zy); values.put("cj",cj); if (reqCode==1) dbHelper.insert("Student",values); else if (reqCode==2){ String ID = intent.getStringExtra("ID"); dbHelper.updateStudent(ID,values); } renderListVIEw(); } }}
activity_manage.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".Manage"> <ListVIEw androID:ID="@+ID/List" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <button androID:ID="@+ID/new_List" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="添加新学生" /></linearLayout>
对应的ListvIEw的布局文件,ListvIEw.xml
因为使用的是数据库来储存信息和调用,并没有重写对于ListvIEw的Adapter,而是使用AndroID自带的SimpleCursorAdapter类方法,用游标来储存,查看数据
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="ID"/> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="姓名" /> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="学号" /> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="班级" /> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="专业" /> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" androID:text="成绩" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:ID="@+ID/ID" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/xm" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/xh" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/bj" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/zy" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> <TextVIEw androID:ID="@+ID/cj" androID:layout_wIDth="0dp" androID:layout_height="50dp" androID:layout_weight="1" /> </linearLayout></linearLayout>
对于新添加学生 *** 作,使用NewStudent.class来完成
package com.example.myapp;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.text.TextUtils;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class NewStudent extends AppCompatActivity { DbHelper dbHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_new_student); dbHelper=new DbHelper(NewStudent.this,"MyDataBase",3); Intent priIntent = getIntent(); EditText et_xm = findVIEwByID(R.ID.et_xm); EditText et_xh = findVIEwByID(R.ID.et_xh); EditText et_bj = findVIEwByID(R.ID.et_bj); EditText et_zy = findVIEwByID(R.ID.et_zy); EditText et_cj = findVIEwByID(R.ID.et_cj); String priID = priIntent.getStringExtra("ID"); String prixm = priIntent.getStringExtra("xm"); String prixh = priIntent.getStringExtra("xh"); String pribj = priIntent.getStringExtra("bj"); String prizy = priIntent.getStringExtra("zy"); String pricj = priIntent.getStringExtra("cj"); et_xm.setText(prixm); et_xh.setText(prixh); et_bj.setText(pribj); et_zy.setText(prizy); et_cj.setText(pricj); button btn_confirm = findVIEwByID(R.ID.btn_confirm); btn_confirm.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { EditText et_xm = findVIEwByID(R.ID.et_xm); EditText et_xh = findVIEwByID(R.ID.et_xh); EditText et_bj = findVIEwByID(R.ID.et_bj); EditText et_zy = findVIEwByID(R.ID.et_zy); EditText et_cj = findVIEwByID(R.ID.et_cj); String xm = et_xm.getText().toString(); String xh = et_xh.getText().toString(); String bj = et_bj.getText().toString(); String zy = et_zy.getText().toString(); String cj = et_cj.getText().toString(); if (TextUtils.isEmpty(xm)||TextUtils.isEmpty(xh)){ Toast.makeText(NewStudent.this,"学号或者姓名不可为空",Toast.LENGTH_SHORT).show(); return; } Intent intent = new Intent(); intent.putExtra("_ID",priID); intent.putExtra("xm",xm); intent.putExtra("xh",xh); intent.putExtra("bj",bj); intent.putExtra("zy",zy); intent.putExtra("cj",cj); setResult(RESulT_OK,intent); finish(); } }); }}
activity_new_student.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:orIEntation="vertical" androID:layout_height="match_parent" tools:context=".NewStudent"> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et_xm" androID:hint="请输入姓名" /> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et_xh" androID:hint="请输入学号" /> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et_bj" androID:hint="请输入班级" /> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et_zy" androID:hint="请输入专业" /> <EditText androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/et_cj" androID:hint="请输入成绩" /> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/btn_confirm" androID:text="确定" /></linearLayout>
运行效果图:
登录页面:
注册界面:
管理界面:
对ListvIEw进行 *** 作:单击
长按
对ListvIEw的添加:
总结以上是内存溢出为你收集整理的AndriodStudio期中利用ListView和数据库实现简单学生管理全部内容,希望文章能够帮你解决AndriodStudio期中利用ListView和数据库实现简单学生管理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)