Android管理系统
一、项目简介
本项目是连接云数据库MySQL设计的一款职工管理系统,基于java语言。
GitHub项目链接: Android职工档案管理系统
二、登录注册功能
1.登录注册功能MySQL用户信息表
列名 | username | password |
---|---|---|
类型 | varchar | varchar |
长度 | 10 | 10 |
2.链接
登录注册功能介绍可以参考我的上一篇博客:链接: Android实现登录注册功能
3.使用介绍
若要正常使用登录注册功能,源码下载后需要修改JDBCUtils.java里的这段代码:
//要连接MySQL数据库的URL URL_MySQL="jdbc:mysql://外网地址:端口/数据库名称"
public static final String URL_MySQL="jdbc:mysql://外网地址:端口/数据库名称";
//要连接MySQL数据库的用户名 NAME_MySQL="MySQL用户名"
public static final String NAME_MySQL="MySQL用户名";
//要连接MySQL数据库的密码 PASSWORD_MySQL="MySQL密码"
public static final String PASSWORD_MySQL="MySQL密码";
“jdbc:mysql://外网地址:端口/数据库名称”,"MySQL用户名"和"MySQL密码"要改成自己的。
三、职工信息管理功能
主页展示:
通过点击添加,删除,修改,查询Button按钮使用Intent进行页面跳转
主要代码如下:
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button_add)
{
Intent intent=new Intent(this,AddActivity.class);
startActivity(intent);
}
if(v.getId()==R.id.button_query)
{
Intent intent=new Intent(this,QueryActivity.class);
startActivity(intent);
}
if(v.getId()==R.id.button_delete)
{
Intent intent=new Intent(this,DeleteActivity.class);
startActivity(intent);
}
if(v.getId()==R.id.button_modify)
{
Intent intent=new Intent(this,ModifyActivity.class);
startActivity(intent);
}
}
布局代码如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:background="#ADD8E6"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="职工档案管理系统"
android:textSize="25sp"
android:textStyle="bold"
android:background="@drawable/shape_rectangle_textview"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="添加"/>
<Button
android:id="@+id/button_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="删除"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button_modify"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="修改"/>
<Button
android:id="@+id/button_query"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="查询"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
1、数据库方面
1.1 MySQL职工信息表
列名 | id | name | sex | department | position | salary | phone |
---|---|---|---|---|---|---|---|
类型 | varchar | varchar | varchar | varchar | varchar | varchar | varchar |
长度 | 10 | 10 | 2 | 10 | 10 | 10 | 10 |
1.2 数据库展示(我这里使用的是腾讯云数据库MySQL)
2、控件方面
1. 控件介绍
简单控件:按钮控件Button,文本内容TextView
中级控件:复选框CheckBox,编辑框EditText,提醒对话框AlertDialog
高级控件:列表视图ListView,基本适配器BaseAdapter
2. 控件调用
1.提醒对话框AlertDialog:提醒对话框可以通过工具类AlertDialogUtils.java调用。
public class AlertDialogUtils {
public static void AlertDialog(Context context,String message)
{
//弹出提醒对话框,提醒用户用户名不能为空
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("尊敬的用户");
builder.setMessage(message);
builder.setPositiveButton("好的",null);
AlertDialog alertDialog=builder.create();
alertDialog.show();
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();
layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
}
}
调用AlertDialog时点击“确定”后可在public void onClick(DialogInterface dialog, int which){}内调用功能函数
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
2.列表视图ListView和基本适配器BaseAdapter的调用用于显示所有职工的信息,在查询功能,删除功能和修改功能都有用到。
3. 布局方面
使用LinearLayout线性布局和RelativeLayout相对布局
4. 代码方面
要使用职工信息管理功能,那么JDBCUtils2.java,JDBCUtils3.java,JDBCUtils4.java,JDBCUtils5.java的"jdbc:mysql://外网地址:端口/数据库名称","MySQL用户名"和"MySQL密码"都要改成自己的。
1. 数据库连接与断开类
1.1 JDBCUtils2.java用于连接MySQL云数据库进行后续职工信息的增删改查
public class JDBCUtils2 {
//要连接MySQL数据库的URL URL_MySQL="jdbc:mysql://外网地址:端口/数据库名称"
public static final String URL_MySQL="jdbc:mysql://外网地址:端口/数据库名称";
//要连接MySQL数据库的用户名 NAME_MySQL="MySQL用户名"
public static final String NAME_MySQL="MySQL用户名";
//要连接MySQL数据库的密码 PASSWORD_MySQL="MySQL密码"
public static final String PASSWORD_MySQL="MySQL密码";
//使用PreparedStatement来执行SQL语句查询
public static PreparedStatement preparedStatement;
//使用Resultset接收JDBC查询语句返回的数据集对象
public static ResultSet resultSet;
//连接数据库
public static Connection connection;
public static void connect2()
{
//开启连接数据库
Log.d("注意","开启连接数据库中......");
connection=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//获取与数据库的连接
connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL);
}catch (Exception e){
//对异常情况进行处理
e.printStackTrace();
}
}
public static void close2()
{
Log.d("注意","正在关闭数据库连接......");
try{
if(resultSet!=null){
resultSet.close();//关闭接收
resultSet=null;
}
if(preparedStatement!=null){
preparedStatement.close();//关闭sql语句查询
preparedStatement=null;
}
if(connection!=null){
connection.close();//关闭数据库连接
connection=null;
}
}catch (Exception e){
//对异常情况进行处理
e.printStackTrace();
}
}
}
1.2 JDBCUtils3.java用于连接MySQL云数据库获取职工总人数
功能函数如下:
public static int getWorkerNumber()
{
//要连接MySQL数据库的URL URL_MySQL=“jdbc:mysql://外网地址:端口/数据 库名称”
final String URL_MySQL=“jdbc:mysql://外网地址:端口/数据库名称”;
//要连接MySQL数据库的用户名 NAME_MySQL=“MySQL用户名”
final String NAME_MySQL=“MySQL用户名”;
//要连接MySQL数据库的密码 PASSWORD_MySQL=“MySQL密码”
final String PASSWORD_MySQL=“MySQL密码”;
connection=null;
int count=0;
try{
//加载驱动
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
//获取与数据库的连接
connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL);
String sql=“SELECT COUNT(*) FROM worker”;
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
while (resultSet.next())
{
count=resultSet.getInt(1);
}
}catch (Exception e){
e.printStackTrace();
}
return count;
}
1.3 JDBCUtils4.java用于连接MySQL云数据库获取职工中男性人数
功能函数如下:
public static int getWorkerMan()
{
//要连接MySQL数据库的URL URL_MySQL=“jdbc:mysql://外网地址:端口/数据库名称”
final String URL_MySQL=“jdbc:mysql://外网地址:端口/数据库名称”;
//要连接MySQL数据库的用户名 NAME_MySQL=“MySQL用户名”
final String NAME_MySQL=“MySQL用户名”;
//要连接MySQL数据库的密码 PASSWORD_MySQL=“MySQL密码”
final String PASSWORD_MySQL=“MySQL密码”;
connection=null;
int count=0;
try{
//加载驱动
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
//获取与数据库的连接
connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL);
String sql=“SELECT COUNT(*) FROM worker WHERE sex = ‘男’”;
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
while (resultSet.next())
{
count=resultSet.getInt(1);
}
}catch (Exception e){
e.printStackTrace();
}
return count;
}
1.4 JDBCUtils5.java用于连接MySQL云数据库获取职工中女性人数
功能函数如下:
public static int getWorkerWoman()
{
//要连接MySQL数据库的URL URL_MySQL=“jdbc:mysql://外网地址:端口/数据库名称”
final String URL_MySQL=“jdbc:mysql://外网地址:端口/数据库名称”;
//要连接MySQL数据库的用户名 NAME_MySQL=“MySQL用户名”
final String NAME_MySQL=“MySQL用户名”;
//要连接MySQL数据库的密码 PASSWORD_MySQL=“MySQL密码”
final String PASSWORD_MySQL=“MySQL密码”;
connection=null;
int count=0;
try{
//加载驱动
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
//获取与数据库的连接
connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL);
String sql=“SELECT COUNT(*) FROM worker WHERE sex = ‘女’”;
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
while (resultSet.next())
{
count=resultSet.getInt(1);
}
}catch (Exception e){
e.printStackTrace();
}
return count;
}
2. 职工信息类
Worker.java
public class Worker implements Serializable {
private String id;//职工编号
private String name;//职工姓名
private String sex;//职工性别
private String department;//职工所在部门
private String position;//职工职位
private String salary;//职工工资
private String phone;//职工电话
public Worker(){
}
public Worker(String id,String name,String sex,String department,String position,String salary,String phone)
{
this.id=id;
this.name=name;
this.sex=sex;
this.department=department;
this.position=position;
this.salary=salary;
this.phone=phone;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getDepartment() {
return department;
}
public String getPosition() {
return position;
}
public String getSalary() {
return salary;
}
public String getPhone() {
return phone;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setDepartment(String department) {
this.department = department;
}
public void setPosition(String position) {
this.position = position;
}
public void setSalary(String salary) {
this.salary = salary;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3. 用户数据操作类
WorkerDao.java
public class WorkerDao extends JDBCUtils2{
public Worker findID(String id)
{
connect2();//获得连接
Worker worker=null;
try{
//处理sql语句。我这里是根据我自己的worker表的id字段来查询记录
String sql="select * from worker where id=?";
//获得预处理对象。获取用于向数据库发送sql语句的preparedStatement
preparedStatement=connection.prepareStatement(sql);
//设置实际参数。用于根据编号进行查询
preparedStatement.setString(1,id);
//执行。执行sql查询语句并返回结果集
resultSet= preparedStatement.executeQuery();
//如果当前记录不是结果集中的最后一行,则进入循环体。
while (resultSet.next())
{
worker=new Worker();
//获取id列列值
worker.setId(resultSet.getString("id"));
}
}catch (Exception e){
//对异常情况进行处理
e.printStackTrace();
}finally {
close2();
}return worker;
}
public int addWorker(Worker worker)
{
int value=0;
connect2();
try {
//处理sql语句。我这里是向我自己worker表的id,name,sex,department,position,salary,phone添加数据
String sql="insert into worker(id,name,sex,department,position,salary,phone) values(?,?,?,?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
//设置实际参数。将数据插入数据库中
preparedStatement.setString(1, worker.getId());//设置编号
preparedStatement.setString(2, worker.getName());//设置姓名
preparedStatement.setString(3, worker.getSex());//设置性别
preparedStatement.setString(4, worker.getDepartment());//设置部门
preparedStatement.setString(5, worker.getPosition());//设置职位
preparedStatement.setString(6, worker.getSalary());//设置工资
preparedStatement.setString(7, worker.getPhone());//设置电话号码
value=preparedStatement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
close2();
}return value;
}
public List<Worker> findWorkerList()
{
List<Worker> list=new ArrayList<>();
connect2();
try{
String sql="select * from worker";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while (resultSet.next())
{
Worker worker=new Worker();
worker.setId(resultSet.getString("id"));
worker.setName(resultSet.getString("name"));
worker.setSex(resultSet.getString("sex"));
worker.setDepartment(resultSet.getString("department"));
worker.setPosition(resultSet.getString("position"));
worker.setSalary(resultSet.getString("salary"));
worker.setPhone(resultSet.getString("phone"));
list.add(worker);
}
}catch (Exception e){
e.printStackTrace();
}finally {
close2();
}return list;
}
public int deleteWorker(String id)
{
int value=0;
try {
connect2();
String sql="delete from worker where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,id);
value=preparedStatement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
close2();
}return value;
}
public int modifyWorker(Worker worker)
{
int value=0;
connect2();
try {
String sql="update worker set name=?,sex=?,department=?,position=?,salary=?,phone=? where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,worker.getName());
preparedStatement.setString(2,worker.getSex());
preparedStatement.setString(3,worker.getDepartment());
preparedStatement.setString(4,worker.getPosition());
preparedStatement.setString(5,worker.getSalary());
preparedStatement.setString(6,worker.getPhone());
preparedStatement.setString(7,worker.getId());
value=preparedStatement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
close2();
}return value;
}
public Worker findID2(String id)
{
connect2();//获得连接
Worker worker=null;
try{
//处理sql语句。我这里是根据我自己的worker表的id字段来查询记录
String sql="select * from worker where id=?";
//获得预处理对象。获取用于向数据库发送sql语句的preparedStatement
preparedStatement=connection.prepareStatement(sql);
//设置实际参数。用于根据编号进行查询
preparedStatement.setString(1,id);
//执行。执行sql查询语句并返回结果集
resultSet= preparedStatement.executeQuery();
//如果当前记录不是结果集中的最后一行,则进入循环体。
while (resultSet.next())
{
String name=resultSet.getString("name");
String sex=resultSet.getString("sex");
String department=resultSet.getString("department");
String position=resultSet.getString("position");
String salary=resultSet.getString("salary");
String phone=resultSet.getString("phone");
worker=new Worker(id,name,sex,department,position,salary,phone);
}
}catch (Exception e){
//对异常情况进行处理
e.printStackTrace();
}finally {
close2();
}return worker;
}
}
4. 添加功能
AddActivity.java
public void add()
{
final String id=editText_worker_id.getText().toString().trim();
final String name=editText_worker_name.getText().toString().trim();
final String sex=editText_worker_sex.getText().toString().trim();
final String department=editText_worker_department.getText().toString().trim();
final String position=editText_worker_position.getText().toString().trim();
final String salary=editText_worker_salary.getText().toString().trim();
final String phone=editText_worker_phone.getText().toString().trim();
if(TextUtils.isEmpty(id))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入编号!");
editText_worker_id.requestFocus();
return;
}else if (TextUtils.isEmpty(name))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入姓名!");
editText_worker_name.requestFocus();
return;
}else if (TextUtils.isEmpty(sex))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入性别!");
editText_worker_sex.requestFocus();
return;
}else if (TextUtils.isEmpty(department))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入部门!");
editText_worker_department.requestFocus();
return;
}else if (TextUtils.isEmpty(position))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入职位!");
editText_worker_position.requestFocus();
return;
}else if (TextUtils.isEmpty(salary))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入工资!");
editText_worker_salary.requestFocus();
return;
}else if (TextUtils.isEmpty(phone))
{
AlertDialogUtils.AlertDialog(AddActivity.this,"请输入电话号码!");
editText_worker_phone.requestFocus();
return;
}else
{
final Worker worker=new Worker();
worker.setId(id);
worker.setName(name);
worker.setSex(sex);
worker.setDepartment(department);
worker.setPosition(position);
worker.setSalary(salary);
worker.setPhone(phone);
new Thread(new Runnable() {
@Override
public void run() {
final int value=workerDao.addWorker(worker);
if(value>0)
{
handler.post(new Runnable() {
@Override
public void run() {
//创建提醒对话框的建造器
AlertDialog.Builder builder=new AlertDialog.Builder(AddActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle("尊敬的用户");
//设置对话框内容文本
builder.setMessage("添加成功!");
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(AddActivity.this,MainActivity.class);
startActivity(intent);
}
});
AlertDialog alertDialog=builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();
layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
return;
}
});
}else
{
Looper.prepare();
AlertDialogUtils.AlertDialog(AddActivity.this,"添加失败!");
Looper.loop();
}
}
}).start();
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button_add_return)
{
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
if(v.getId()==R.id.button_add_right)
{
final String id=editText_worker_id.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
final Worker worker_id=workerDao.findID(id);
handler.post(new Runnable() {
@Override
public void run() {
if (worker_id!=null)
{
//创建提醒对话框的建造器
AlertDialog.Builder builder=new AlertDialog.Builder(AddActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle("尊敬的用户");
//设置对话框内容文本
builder.setMessage("您所输入的职工编号已存在,请重新输入!");
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText_worker_id.setText("");//清空编号编辑框内容
editText_worker_name.setText("");//清空姓名编辑框内容
editText_worker_sex.setText("");//清空性别编辑框内容
editText_worker_department.setText("");//清空部门编辑框内容
editText_worker_position.setText("");//清空职位编辑框内容
editText_worker_salary.setText("");//清空工资编辑框内容
editText_worker_phone.setText("");//清空电话编辑框内容
}
});
AlertDialog alertDialog=builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();
layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
return;
}else
{
add();
}
}
});
}
}).start();
}
}
activity_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:background="#ADD8E6"
android:orientation="vertical"
tools:context=".AddActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加职工档案信息"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold"
android:background="@drawable/shape_rectangle_textview">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="编号:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="姓名:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="性别:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_sex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="部门:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_department"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="职位:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_position"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="工资:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_salary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:text="电话:"
android:gravity="center|bottom"/>
<EditText
android:id="@+id/editText_worker_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button_add_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="20dp"
android:text="返回"/>
<Button
android:id="@+id/button_add_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="20dp"
android:text="确认添加"/>
</LinearLayout>
</LinearLayout>
5. 删除功能
WorkerBaseAdapter2
public class WorkerBaseAdapter2 extends BaseAdapter {
private Context context;//声明一个上下文对象
private List<Worker> workerList;//声明一个职工信息列表
private buttonListDeleteOnClickListener buttonListDeleteOnClickListener;
private Button button_list_delete;
public WorkerBaseAdapter2(){
}
//职工适配器的构造方法,传入上下文与职工列表
public WorkerBaseAdapter2(Context context,List<Worker> workerList)
{
this.context=context;
this.workerList=workerList;
}
public void setWorkerList(List<Worker> workerList) {
this.workerList = workerList;
}
public void setButtonListDeleteOnClickListener(buttonListDeleteOnClickListener buttonListDeleteOnClickListener) {
this.buttonListDeleteOnClickListener = buttonListDeleteOnClickListener;
}
//获取列表项的个数
@Override
public int getCount() {
return workerList.size();
}
//获取列表项的数据
@Override
public Object getItem(int position) {
return workerList.get(position);
}
//获取列表项的编号
@Override
public long getItemId(int position) {
return position;
}
//获取指定位置的列表视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
WorkerBaseAdapter2.ViewHolder viewHolder;
//如果转换视图为空
if(convertView==null)
{
viewHolder=new WorkerBaseAdapter2.ViewHolder();//创建一个新的视图持有者
//根据布局文件worker_listview_delete.xml生成转换视图对象
convertView= LayoutInflater.from(context).inflate(R.layout.worker_listview_delete,null);
viewHolder.textView_query_id2=convertView.findViewById(R.id.textview_query_id2);
viewHolder.textView_query_name2=convertView.findViewById(R.id.textview_query_name2);
viewHolder.textView_query_sex2=convertView.findViewById(R.id.textview_query_sex2);
viewHolder.textView_query_department2=convertView.findViewById(R.id.textview_query_department2);
viewHolder.textView_query_position2=convertView.findViewById(R.id.textview_query_position2);
viewHolder.textView_query_salary2=convertView.findViewById(R.id.textview_query_salary2);
viewHolder.textView_query_phone2=convertView.findViewById(R.id.textview_query_phone2);
viewHolder.button_list_delete=convertView.findViewById(R.id.button_list_delete);
convertView.setTag(viewHolder);//将视图持有者保存到转换视图当中
}else
{
//如果转换视图非空,则从转换视图中获取之前保存的视图持有者
viewHolder=(WorkerBaseAdapter2.ViewHolder) convertView.getTag();
}
Worker worker=workerList.get(position);
viewHolder.textView_query_id2.setText(worker.getId());//显示职工编号
viewHolder.textView_query_name2.setText(worker.getName());//显示职工姓名
viewHolder.textView_query_sex2.setText(worker.getSex());//显示职工性别
viewHolder.textView_query_department2.setText(worker.getDepartment());//显示职工部门
viewHolder.textView_query_position2.setText(worker.getPosition());//显示职工职位
viewHolder.textView_query_salary2.setText(worker.getSalary());//显示职工工资
viewHolder.textView_query_phone2.setText(worker.getPhone());//显示职工电话
viewHolder.button_list_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonListDeleteOnClickListener.buttonListDeleteOnClickListener(v,position);
}
});
return convertView;
}
//定义一个视图持有者,以便重用列表项的视图资源
public final class ViewHolder
{
public TextView textView_query_id2;//声明职工编号的文本视图对象
public TextView textView_query_name2;//声明职工姓名的文本视图对象
public TextView textView_query_sex2;//声明职工性别的文本视图对象
public TextView textView_query_department2;//声明职工部门的文本视图对象
public TextView textView_query_position2;//声明职工职位的文本视图对象
public TextView textView_query_salary2;//声明职工工资的文本视图对象
public TextView textView_query_phone2;//声明职工电话的文本视图对象
private Button button_list_delete;//声明删除职工按钮
}
}
DeleteActivity.java
public void delete()
{
final String id=editText_delete_by_id.getText().toString().trim();
if(TextUtils.isEmpty(id))
{
AlertDialogUtils.AlertDialog(DeleteActivity.this,"请输入编号!");
editText_delete_by_id.requestFocus();
return;
}else
{
new Thread(new Runnable() {
@Override
public void run() {
final int value=workerDao.deleteWorker(id);
if(value>0)
{
handler.post(new Runnable() {
@Override
public void run() {
//创建提醒对话框的建造器
AlertDialog.Builder builder=new AlertDialog.Builder(DeleteActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle("尊敬的用户");
//设置对话框内容文本
builder.setMessage("删除成功!");
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new Thread(new Runnable() {
@Override
public void run() {
workerList=workerDao.findWorkerList();
handler.post(new Runnable() {
@Override
public void run() {
if(workerBaseAdapter2==null)
{
workerBaseAdapter2=new WorkerBaseAdapter2(DeleteActivity.this,workerList);
listView_all_worker2.setAdapter(workerBaseAdapter2);
}else
{
workerBaseAdapter2.setWorkerList(workerList);
workerBaseAdapter2.notifyDataSetChanged();
}
}
});
}
}).start();
}
});
AlertDialog alertDialog=builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();
layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
return;
}
});
}else
{
Looper.prepare();
AlertDialogUtils.AlertDialog(DeleteActivity.this,"删除失败!该职工编号不存在!请重新输入");
Looper.loop();
}
}
}).start();
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button_delete_return)
{
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
if(v.getId()==R.id.button_delete_by_id)
{
delete();
editText_delete_by_id.setText("");
}
}
new Thread(new Runnable() {
@Override
public void run() {
workerList=workerDao.findWorkerList();
handler.post(new Runnable() {
@Override
public void run() {
if(workerBaseAdapter2null)
{
workerBaseAdapter2=new WorkerBaseAdapter2(DeleteActivity.this,workerList);
listView_all_worker2.setAdapter(workerBaseAdapter2);
}else
{
workerBaseAdapter2.setWorkerList(workerList);
workerBaseAdapter2.notifyDataSetChanged();
}
workerBaseAdapter2.setButtonListDeleteOnClickListener(new buttonListDeleteOnClickListener() {
@Override
public void buttonListDeleteOnClickListener(View view, int position) {
final Worker worker=workerList.get(position);
//创建提醒对话框的建造器
AlertDialog.Builder builder=new AlertDialog.Builder(DeleteActivity.this);
//设计对话框标题图标
builder.setIcon(R.mipmap.ic_launcher);
//设置对话框标题文本
builder.setTitle(“尊敬的用户”);
//设置对话框内容文本
builder.setMessage(“您确定要删除职工编号为”+worker.getId()+“的职工吗?”);
//设置对话框的肯定按钮文本及其点击监听器
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new Thread(new Runnable() {
@Override
public void run() {
final int value=workerDao.deleteWorker(worker.getId());
handler.post(new Runnable() {
@Override
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
workerList=workerDao.findWorkerList();
handler.post(new Runnable() {
@Override
public void run() {
if(workerBaseAdapter2null)
{
workerBaseAdapter2=new WorkerBaseAdapter2(DeleteActivity.this,workerList);
listView_all_worker2.setAdapter(workerBaseAdapter2);
}else
{
workerBaseAdapter2.setWorkerList(workerList);
workerBaseAdapter2.notifyDataSetChanged();
}
}
});
}
}).start();
}
});
}
}).start();
}
});
builder.setNegativeButton(“取消”,null);
AlertDialog alertDialog=builder.create();//根据建造器构建提醒对话框对象
alertDialog.show();//显示提醒对话框
//设计AlertDialog提醒对话框大小
WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();
layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//设置AlertDialog的宽高
}
});
}
});
}
}).start();
worker_listview_delete.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:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="编号:"/>
<TextView
android:id="@+id/textview_query_id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="left"/>
</LinearLayout>
<Button
android:id="@+id/button_list_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="删除"
android:backgroundTint="#FF0000">
</Button>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="姓名:"/>
<TextView
android:id="@+id/textview_query_name2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="性别:"/>
<TextView
android:id="@+id/textview_query_sex2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="部门:"/>
<TextView
android:id="@+id/textview_query_department2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="职位:"/>
<TextView
android:id="@+id/textview_query_position2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="工资:"/>
<TextView
android:id="@+id/textview_query_salary2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="电话:"/>
<TextView
android:id="@+id/textview_query_phone2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>
activivty_delete.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=".DeleteActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#66BB">
<Button
android:id="@+id/button_delete_return"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="返回"/>
<EditText
android:id="@+id/editText_delete_by_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:maxLength="9"
android:hint="请输入要删除职工的编号"
android:textColorHint="#999999"
android:background="@drawable/shape_round_rectangle"/>
<Button
android:id="@+id/button_delete_by_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"/>
</LinearLayout>
<ListView
android:id="@+id/listView_all_worker2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
6. 修改功能
7. 查询功能
由于篇幅原因就不多做展示了