本文章是上一篇的续写,新增了对云平台表数据信息的功能,实现了删除、修改和添加
环境配置搭建等前期需要的东西不再阐述,有需求的请看上一篇内容
但由于我写这篇文章的时候,我的免费试用云数据库已经过期了,因此没有最终效果图供参考
目录
2、activity_student_list_item.xml
1、修改界面activity_student_edit.xml
2、添加界面activity_student_add.xml
一、Android Studio界面设计部分
该界面设计是xml文件
(一)信息管理主界面
在信息管理界面中,不仅能显示已存在的表信息,还能在其基础上进行删除、修改和添加功能
其中修改和添加需要分别设计一个新的界面用于编辑信息,在后文会提到
1、activity_student_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=".Student_Manage">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text="成绩信息"
android:textSize="30sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="7dp">
<ImageView
android:id="@+id/btn_return"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/bacl_foreground" />
<ImageView
android:id="@+id/btn_add"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="300dp"
android:src="@drawable/add1_foreground" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/xs_bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_xh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_xm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_cj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成绩"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_students"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
代码中包含的图片文件:
在红线勾画的两个部分分别对应返回图片按钮和添加图片按钮
当然包括后面会提到的编辑和删除图片按钮,这些按钮都可以在系统中设置(见步骤)
步骤:
1、在如图下图的drawable点击鼠标右键,new一个image Asset
2、然后在以下界面修改图片名字,比如这里是删除delete,是一个垃圾桶的图标
修改完名字后点击下图中的第2步,也就是垃圾桶图标的位置
3、在该界面的搜索框输入edit(或者delete、add、back),选中你喜欢的图标,然后点击OK
4、也可以选择你喜欢的颜色,当然还有其他的设置
5、然后点击Next
6、如果你的界面中没有红色,点击Finish即可
我这里命名重复了,因此是红色的,如果这种情况仍还要Finish的话就会覆盖原来该命名下的图标
为避免覆盖,可以修改以下命名
图片文件所在位置如图:
(1)在xml文件中:
(2)在目录中:
效果如图:
注:最终运行虚拟机后会有编辑和删除的图标,在下一个xml文件即可看到
2、activity_student_list_item.xml
该xml文件的功能在上一篇中提过,不再说明
代码:(图标按钮需自行添加到目录)
<?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="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/xs_bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_xh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_xm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
<TextView
android:id="@+id/xs_cj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成绩"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
<ImageView android:id="@+id/btn_student_list_edit"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/edit_foreground"/>
<ImageView android:id="@+id/btn_student_list_delete"
android:layout_marginLeft="5dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/delete_foreground"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
效果如图:
可以看到编辑和删除图标绑定了每一个item,因此最终在虚拟机上运行出来的结果是每一行数据信息都有编辑和删除图标,以此针对每一条信息的编辑和删除
(二)信息修改和添加界面
1、修改界面activity_student_edit.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:layout_margin="5dp"
android:gravity="center|top"
android:orientation="vertical"
tools:context=".student_edit">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text="修改学生成绩"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生班级:"/>
<EditText
android:id="@+id/et_student_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:focusable="false"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生班级"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生学号:"/>
<EditText
android:id="@+id/et_student_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:focusable="false"
android:textSize="16sp"
android:hint="请输入学生学号"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生姓名:"/>
<EditText
android:id="@+id/et_student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:focusable="false"
android:textSize="16sp"
android:hint="请输入学生姓名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生成绩:"/>
<EditText
android:id="@+id/et_student_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生成绩"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_ok_click"
android:layout_marginTop="35dp"
android:text="确 定"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="OnClick" />
</LinearLayout>
注意!在该界面下我仅设置了最后一行编辑框可编辑(即“请输入学生成绩”编辑框)
而我在设置时在前三行中加入了下图红线勾画的代码(按需添加即可)
效果如图:
2、添加界面activity_student_add.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:layout_margin="5dp"
android:gravity="center|top"
android:orientation="vertical"
tools:context=".StudentAdd">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text="添加学生信息"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生班级:"/>
<EditText
android:id="@+id/et_student_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生班级"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生学号:"/>
<EditText
android:id="@+id/et_student_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生学号"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生姓名:"/>
<EditText
android:id="@+id/et_student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生姓名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:text="学生成绩:"/>
<EditText
android:id="@+id/et_student_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="16sp"
android:hint="请输入学生成绩"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_ok_click"
android:layout_marginTop="35dp"
android:text="确 定"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="OnClick" />
</LinearLayout>
该界面的所有编辑框都是可编辑的
效果如图:
二、Android Studio与云数据库交互部分
(一)连接部分
该部分大部分内容和上一篇文章的交互部分相同,细节的地方不再详细讲解
1、DbOpenHelper.java
具体可见上一篇文章,这里不再附上代码
2、XXInfo.java
XXinfo中的XX可以是任意的名称,在这里用我的文件举例
我的文件名是StudentInfo,位置同DbOpenHelper
代码:
package com.example.helloword;
import java.io.Serializable;
public class StudentInfo implements Serializable {
private String sconum;
private String clanum;
private String snum;
private String sname;
private String score;
public StudentInfo() {
}
public StudentInfo(String sconum, String clanum, String snum, String sname,String score) {
this.sconum = sconum;
this.clanum=clanum;
this.snum=snum;
this.sname=sname;
this.score=score;
}
//
public String getSconum() {
return sconum;
}
public void setSconum(String sconum) {
this.sconum = sconum;
}
//
public String getClanum() {
return clanum;
}
public void setClanum(String clanum) {
this.clanum = clanum;
}
//
public String getSnum() {
return snum;
}
public void setSnum(String snum) {
this.snum = snum;
}
//
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
//
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String toString() {
return "StudentInfo{" +
"sconum='" + sconum + '\'' +
", clanum='" + clanum + '\'' +
", snum='" + snum + '\'' +
", sname='" + sname + '\'' +
", score='" + score + '\'' +
'}';
}
}
3、XXInfoDao.java
XXinfoDao中的XX可以是任意的名称,在这里用我文件的举例
我的文件名是StudentInfoDao,位置同DbOpenHelper
代码:
package com.example.helloword;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* 学生信息业务层
* 实现对学生信息的增删改查
*/
public class StudentInfoDao extends DbOpenHelper{
StudentInfo item = null;
/**
*查找全部
* @return 返回用户名和密码的列表
*/
public List<StudentInfo> getAllStudentList(){
List<StudentInfo> list = new ArrayList<>();
try {
getConnection(); // 取得连接信息
String sql = "select * from scores";
pStmt = conn.prepareStatement(sql);
// 填充 第一个 uname
rs = pStmt.executeQuery();//执行sql语句
//如果有查询结果,则执行 if 代码块中的语句,否则不执行
while (rs.next()){
StudentInfo item = new StudentInfo();
item.setSconum(rs.getString("sconum"));
item.setClanum(rs.getString("clanum"));
item.setSnum(rs.getString("snum"));
item.setSname(rs.getString("sname"));
item.setScore(rs.getString("score"));
list.add(item);
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return list;
}
/**
* 按用户名和密码查询信息 用于登录在
* @return 返回的为NamePassInfo的实例
*/
public StudentInfo getStudent(String sconum, String clanum,String snum,String sname,String score){
StudentInfo item=null;
try {
getConnection(); // 取得连接信息
String sql = "select * from scores where sconum=? clanum=? snum=? sname=? and score=?";
pStmt = conn.prepareStatement(sql);
// 填充 第一个 uname
pStmt.setString(1, clanum);
// 填充 第二个 upass
pStmt.setString(2, snum);
// 填充 第三个 uname
pStmt.setString(3, sname);
// 填充 第四个 uname
pStmt.setString(4, score);
rs = pStmt.executeQuery();//执行sql语句
//如果有查询结果,则执行 if 代码块中的语句,否则不执行
if(rs.next()){
item = new StudentInfo();
item.setSconum(rs.getString("sconum"));
item.setClanum(rs.getString("clanum"));
item.setSnum(rs.getString("snum"));
item.setSname(rs.getString("sname"));
item.setScore(rs.getString("score"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return item;
}
/**
* 添加用户信息
* @param
* @return 返回影响的行数
*/
public int addStudent(StudentInfo studentInfo){
int iRow = 0;
try{
getConnection(); //取得连接
Log.i("数据sql","" + studentInfo.getSconum() + " " + studentInfo.getClanum() + " " + studentInfo.getSnum() + " " + studentInfo.getSname() + " " + studentInfo.getScore());
String sql = "insert into scores (sconum, clanum, snum, sname, score) values(?,?,?,?,?)";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, studentInfo.getSconum());
pStmt.setString(2, studentInfo.getClanum());
pStmt.setString(3, studentInfo.getSnum());
pStmt.setString(4, studentInfo.getSname());
pStmt.setString(5, studentInfo.getScore());
iRow = pStmt.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return iRow;
}
public int editStudent(StudentInfo studentEdit) {
int iRow = 0;
try{
getConnection(); //取得连接
// 此处必须要有id,要不然则为全部修改
String sql = "update scores set clanum=?, snum=?, sname=?, score=? where sconum=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, studentEdit.getClanum());
pStmt.setString(2, studentEdit.getSnum());
pStmt.setString(3, studentEdit.getSname());
pStmt.setString(4, studentEdit.getScore());
pStmt.setString(5, studentEdit.getSconum());
iRow = pStmt.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return iRow;
}
public int delStudent(String sconum) {
int iRow = 0;
try{
getConnection(); //取得连接
String sql = "delete from scores where sconum = ?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, sconum);
iRow = pStmt.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return iRow;
}
}
4、LvXXInfoAdapter.java
LvXXInfoAdapter中的XX可以是任意的名称,在这里用我文件的举例
我的文件名是LvStudentInfoAdapter,位置同DbOpenHelper
代码:
package com.example.helloword;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* 自定义学生用户数据适配器类
*/
public class LvStudentinfoAdapter extends BaseAdapter {
private Context context; // 上下文管理信息(谁调用这个适配器谁就是上下文,在本个中是管理员的学生信息管理功能)
private List<StudentInfo> studentList; // 学生信息数据集合
private OnEditBtnClickListener onEditBtnClickListener; // 修改按钮的点击事件监听实例
private OnDelBtnClickListener onDelBtnClickListener; // 删除按钮的点击事件监听实例
public LvStudentinfoAdapter() {
}
public LvStudentinfoAdapter(Context context, List<StudentInfo> studentList) {
this.context = context;
this.studentList = studentList;
Log.i("数据库条数",":" + studentList.size());
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public List<StudentInfo> getStudentList() {
return studentList;
}
public void setStudentList(List<StudentInfo> studentList) {
this.studentList = studentList;
}
public OnEditBtnClickListener getOnEditBtnClickListener() {
return onEditBtnClickListener;
}
public void setOnEditBtnClickListener(OnEditBtnClickListener onEditBtnClickListener) {
this.onEditBtnClickListener = onEditBtnClickListener;
}
public OnDelBtnClickListener getOnDelBtnClickListener() {
return onDelBtnClickListener;
}
public void setOnDelBtnClickListener(OnDelBtnClickListener onDelBtnClickListener) {
this.onDelBtnClickListener = onDelBtnClickListener;
}
@Override
public int getCount() {
return studentList.size();
}
@Override
public Object getItem(int position) {
return studentList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
// 从context页面获取信息进行反射inflate
convertView = LayoutInflater.from(context).inflate(R.layout.activity_student_list_item, null);
viewHolder = new ViewHolder();
viewHolder.xs_bj = convertView.findViewById(R.id.xs_bj);
viewHolder.xs_xh = convertView.findViewById(R.id.xs_xh);
viewHolder.xs_xm = convertView.findViewById(R.id.xs_xm);
viewHolder.xs_cj = convertView.findViewById(R.id.xs_cj);
viewHolder.btn_edit = convertView.findViewById(R.id.btn_student_list_edit);
viewHolder.btn_delete = convertView.findViewById(R.id.btn_student_list_delete);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
// 这里进行数据填充
StudentInfo item = studentList.get(position);
viewHolder.xs_bj.setText(item.getClanum());
viewHolder.xs_xh.setText(item.getSnum());
viewHolder.xs_xm.setText(item.getSname());
viewHolder.xs_cj.setText(item.getScore());
// 修改按钮的点击事件
viewHolder.btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onEditBtnClickListener.onEditBtnClick(v, position);
}
});
// 删除按钮的点击事件
viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDelBtnClickListener.onDelBtnClick(v, position);
}
});
return convertView;
}
// 自定义内部类
private class ViewHolder{
private TextView xs_bj,xs_xh,xs_xm,xs_cj;
private ImageView btn_edit, btn_delete;
}
}
需注意,在LvStudentInfoAdapter.java文件中,有两个接口文件是不存在的,会出现红色的下划线
分别是OnDelBtnClickListener.java和OnEditBtnClickListener.java
选中这两个红色的区域,使用快捷键 Alt + Enter 出现几个选项,如下图
选择 Create interface
但是生成的接口文件可能不对,因此我还是给出正确的接口文件
代码:
(1)OnDelBtnClickListener.java
package com.example.helloword;
import android.view.View;
/**
* 删除按钮的点击事件监听接口
*/
public interface OnDelBtnClickListener {
void onDelBtnClick(View view, int position); // 删除按钮的点击事件处理
}
(2)OnEditBtnClickListener.java
package com.example.helloword;
import android.view.View;
/**
* 修改按钮的点击事件监听接口
*/
public interface OnEditBtnClickListener {
void onEditBtnClick(View view, int position); // 修改按钮的点击事件处理
}
这两个接口文件位置同DbOpenHelper
(二)界面对应java文件部分
1、Student_Manage.java
代码:
package com.example.helloword;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import java.io.Serializable;
import java.util.List;
public class Student_Manage extends AppCompatActivity {
private ImageView btn_return, btn_add;
private ListView lv_students;// 学生列表组件
private Handler mainHandler; // 主线程
private StudentInfoDao studentInfoDao; // 学生数据库操作实例
private List<StudentInfo> studentslist; // 学生信息列表
private LvStudentinfoAdapter lvStudentinfoAdapter; // 用户信息适配器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__manage);
initViews(); // 实例化页面组件
loadStudentDb();
}
private void initViews() {
btn_return = findViewById(R.id.btn_return);
//btn_add = findViewById(R.id.btn_add);
lv_students = findViewById(R.id.lv_students);
mainHandler = new Handler(getMainLooper());// 获取主线程
studentInfoDao = new StudentInfoDao();
btn_return.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
/*
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Student_Manage.this, StudentAdd.class);
startActivityForResult(intent, 1);
}
});
// lv_students.setOnClickListener(this);
*/
}
private void loadStudentDb() {
new Thread(new Runnable() {
@Override
public void run() {
studentslist = studentInfoDao.getAllStudentList(); // 获取所有的用户数据
mainHandler.post(new Runnable() {
@Override
public void run() {
showLvStuData();
}
});
}
}).start();
}
// 显示学生数据
private void showLvStuData(){
// 当前界面为空时(即首次加载)
if(lvStudentinfoAdapter == null){
lvStudentinfoAdapter = new LvStudentinfoAdapter(this, studentslist);
lv_students.setAdapter(lvStudentinfoAdapter);
}else{ // 当数据更新时
lvStudentinfoAdapter.setStudentList(studentslist);
lvStudentinfoAdapter.notifyDataSetChanged();
}
// 修改按钮的操作
lvStudentinfoAdapter.setOnEditBtnClickListener(new OnEditBtnClickListener() {
@Override
public void onEditBtnClick(View view, int position) {
Log.i("修改","");
StudentInfo item = studentslist.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable("studentEdit", item);
Intent intent = new Intent(Student_Manage.this, student_edit.class);
intent.putExtras(bundle);
startActivityForResult(intent, 1);
}
});
// 删除按钮的操作
lvStudentinfoAdapter.setOnDelBtnClickListener(new OnDelBtnClickListener() {
@Override
public void onDelBtnClick(View v, int position) {
// 删除方法
final StudentInfo item = studentslist.get(position);
new AlertDialog.Builder(Student_Manage.this)
.setTitle("删除确认")
.setMessage("您确定要删除:name:[" + item.getSname() + "]的用户信息吗")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doDelStu(item.getSnum());
}
})
.setNegativeButton("取消", null)
.create().show();
}
});
}
/**
* 执行删除用户的方法
*/
private void doDelStu(final String snum){
new Thread(new Runnable() {
@Override
public void run() {
final int iRow = studentInfoDao.delStudent(snum);
mainHandler.post(new Runnable() {
@Override
public void run() {
loadStudentDb();
}
});
}
}).start();
}
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == 1){ //操作成功
loadStudentDb(); // 重新加载数据
}
}
}
2、StudentAdd.java
代码:
package com.example.helloword;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import java.util.UUID;
public class StudentAdd extends AppCompatActivity {
private static final java.util.UUID UUID = null;
private EditText et_student_class, et_student_num, et_student_name,et_student_score;
private StudentInfoDao studentInfoDao; // 用户数据操作类实例
private Handler mainHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_add);
et_student_class = findViewById(R.id.et_student_class);
et_student_num = findViewById(R.id.et_student_num);
et_student_name = findViewById(R.id.et_student_name);
et_student_score = findViewById(R.id.et_student_score);
studentInfoDao = new StudentInfoDao();
mainHandler = new Handler(getMainLooper());
}
// 确定按钮的点击事件
public void btn_ok_click(View view) {
final String uclass = et_student_class.getText().toString().trim();
final String unum = et_student_num.getText().toString().trim();
final String uname = et_student_name.getText().toString().trim();
final String uscore = et_student_score.getText().toString().trim();
// 如果为空
if(TextUtils.isEmpty(uclass)){
CommonUtils.showShortMsg(this, "请输入学生班级");
et_student_class.requestFocus();
}
else if(TextUtils.isEmpty(unum)){
CommonUtils.showShortMsg(this, "请输入学生学号");
et_student_num.requestFocus();
}
else if(TextUtils.isEmpty(uname)) {
CommonUtils.showShortMsg(this, "请输入学生姓名");
et_student_name.requestFocus();
}
else if(TextUtils.isEmpty(uscore)){
CommonUtils.showShortMsg(this, "请输入学生成绩");
et_student_score.requestFocus();
}
else{
final StudentInfo item = new StudentInfo();
String sconum = java.util.UUID.randomUUID().toString();
item.setSconum(sconum);
item.setClanum(uclass);
item.setSnum(unum);
item.setSname(uname);
item.setScore(uscore);
//item.setDate(CommonUtils.getDateStrFromNow)
new Thread(new Runnable() {
@Override
public void run() {
final int iRow = studentInfoDao.addStudent(item);
// 获取之后调用主线程(主线程就是界面的这个线程)
mainHandler.post(new Runnable() {
@Override
public void run() {
setResult(1); // 使用参数表示当前界面操作成功,并返回学生管理界面
finish();
}
});
}
}).start();
}
}
}
3、student_edit.java
代码:
package com.example.helloword;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
public class student_edit extends AppCompatActivity {
private EditText et_student_class, et_student_num, et_student_name,et_student_score;
private StudentInfoDao studentInfoDao; // 用户数据操作类实例
private StudentInfo studentEdit; // 当前要修改的用户信息
private Handler mainHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_edit);
et_student_class = findViewById(R.id.et_student_class);
et_student_num = findViewById(R.id.et_student_num);
et_student_name = findViewById(R.id.et_student_name);
et_student_score = findViewById(R.id.et_student_score);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
studentEdit = (StudentInfo) bundle.getSerializable("studentEdit");
et_student_class.setText(studentEdit.getClanum());
et_student_num.setText(studentEdit.getSnum());
et_student_name.setText(studentEdit.getSname());
et_student_score.setText(studentEdit.getScore());
}
studentInfoDao = new StudentInfoDao();
mainHandler = new Handler(getMainLooper());
}
// 确定按钮的点击事件
public void btn_ok_click(View view) {
final String uclass = et_student_class.getText().toString().trim();
final String unum = et_student_num.getText().toString().trim();
final String uname = et_student_name.getText().toString().trim();
final String uscore = et_student_score.getText().toString().trim();
// 如果为空
if(TextUtils.isEmpty(uclass)){
CommonUtils.showShortMsg(this, "请输入学生班级");
et_student_class.requestFocus();
}
else if(TextUtils.isEmpty(unum)){
CommonUtils.showShortMsg(this, "请输入学生学号");
et_student_num.requestFocus();
}
else if(TextUtils.isEmpty(uname)){
CommonUtils.showShortMsg(this, "请输入学生姓名");
et_student_name.requestFocus();
}
else if(TextUtils.isEmpty(uscore)){
CommonUtils.showShortMsg(this, "请输入学生成绩");
et_student_score.requestFocus();
}
else{
studentEdit.setClanum(uclass);
studentEdit.setSnum(unum);
studentEdit.setSname(uname);
studentEdit.setScore(uscore);
// item.setDate(CommonUtils.getDateStrFromNow)
new Thread(new Runnable() {
@Override
public void run() {
final int iRow = studentInfoDao.editStudent(studentEdit);
// 获取之后调用主线程(主线程就是界面的这个线程)
mainHandler.post(new Runnable() {
@Override
public void run() {
setResult(1); // 使用参数表示当前界面操作成功,并返回学生管理界面
finish();
}
});
}
}).start();
}
}
}
三、一些问题
1、实际上,从某种程度来说,本文章实现的功能有些可能无法实现,但是吧,同样的代码我室友的电脑能运行,这我也解释不了了QAQ
2、另外就是开篇我说过我的云数据库过期了,因此无法验证是否存在问题,算是比较大的一个问题了(但是写这个文章好累啊www放过孩子吧,卖萌.jpg)
3、搜索功能我考虑考虑要不要写吧嘻嘻