AndriodStudio利用ListView和数据库实现简单学生管理


当前第2页 返回上一页

因为使用的是数据库来储存信息和调用,并没有重写对于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的添加:


标签:SQLite

返回前面的内容

相关阅读 >>

python编写通讯录通过数据库存储实现模糊查询功能

Sqlitestudio打开后如何切换成简体中文Sqlitestudio绿色版中文设置方法介绍

sql在一个表中添加字段并添加备注的方法

linux Sqlite3 基本命令

Sqlite3中的日期时间函数使用小结

Sqlite 常用函数 推荐

python数据库如何连接Sqlite详解

射手播放器字幕保存路径怎么修改

scp远程vps快速搬家和wdcp升级php5.3安装memcached和eaccelerator教程

Sqlite数据库管理相关命令的使用介绍

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...