数据库的创建
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的添加:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。