Android management system

1. Project Introduction

This project is an employee management system designed to connect to the cloud database MySQL, based on the java language.
GitHub project link: Android employee file management system

2. Login and registration function

1. Login registration function MySQL user information table

column name username password
type varchar varchar
length 10 10

2. Link
For an introduction to the login and registration functions, please refer to my previous blog: Link: Android implements the login and registration function
3. Introduction
To use the login and registration function normally, you need to modify this code in JDBCUtils.java after downloading the source code:

//要连接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://external network address: port/database name", "MySQL user name" and "MySQL password" should be changed to your own.

3. Employee information management function

Home page display:
main.png
Click to add, delete, modify, query Button button to use Intent to jump to the page.
The main code is as follows:
@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);
}
}

The layout code is as follows:
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. Database

1.1 MySQL employee information table

column name id name sex department position salary phone
type varchar varchar varchar varchar varchar varchar varchar
length 10 10 2 10 10 10 10

1.2 Database display (I use Tencent Cloud Database MySQL here)
MySQL.png

2. Controls

1. Control introduction

Simple control: button control Button, text content TextView
Intermediate control: check box CheckBox, edit box EditText, reminder dialog box AlertDialog
Advanced control: list view ListView, basic adapter BaseAdapter

2. Control call

1. Alert dialog box AlertDialog: The alert dialog box can be called through the tool class 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的宽高
    }
}

After clicking "OK" when calling AlertDialog, the function function can be called in public void onClick(DialogInterface dialog, int which){}

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
    
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
    
    
                                    }
                                });

2. The call of the list view ListView and the basic adapter BaseAdapter is used to display the information of all employees, and it is used in the query function, delete function and modify function.

3. Layout

Use LinearLayout linear layout and RelativeLayout relative layout

4. Code aspects

To use the employee information management function, "jdbc:mysql://external network address: port/database name", "MySQL user name" and "MySQL password" of JDBCUtils2.java, JDBCUtils3.java, JDBCUtils4.java, JDBCUtils5.java "Make it your own.

1. Database connection and disconnection class

1.1 JDBCUtils2.java is used to connect to the MySQL cloud database for subsequent addition, deletion, modification and query of employee information

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 is used to connect to the MySQL cloud database to obtain the total number of employees.
The function is as follows:
public static int getWorkerNumber()
{ // URL to connect to the MySQL database URL_MySQL="jdbc:mysql://external network address: port/database name " final String URL_MySQL="jdbc:mysql://external network address: port/database name"; // User name to connect to MySQL database NAME_MySQL="MySQL user name" final String NAME_MySQL="MySQL user name"; // The password to connect to the MySQL database PASSWORD_MySQL="MySQL password" final String PASSWORD_MySQL="MySQL password"; connection=null; int count=0; try{ // load the driver Class.forName("com.mysql.jdbc.Driver") .newInstance(); //Get the connection with the database 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 is used to connect to the MySQL cloud database to obtain the number of male employees. The
function is as follows:
public static int getWorkerMan()
{ //URL to connect to the MySQL database URL_MySQL="jdbc:mysql://external network address: port/database Name" final String URL_MySQL="jdbc:mysql://external network address: port/database name"; // User name to connect to the MySQL database NAME_MySQL="MySQL user name" final String NAME_MySQL="MySQL user name"; / /The password to connect to the MySQL database PASSWORD_MySQL="MySQL password" final String PASSWORD_MySQL="MySQL password"; connection=null; int count=0; try{ //load the driver Class.forName("com.mysql.jdbc.Driver" ).newInstance(); //Get the connection with the database connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL); String sql="SELECT COUNT(*) FROM worker WHERE sex = 'Male'";














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 is used to connect to the MySQL cloud database to obtain the number of female employees.
The function is as follows:
public static int getWorkerWoman()
{ // URL to connect to the MySQL database URL_MySQL="jdbc:mysql://external network address: port/database Name" final String URL_MySQL="jdbc:mysql://external network address: port/database name"; // User name to connect to the MySQL database NAME_MySQL="MySQL user name" final String NAME_MySQL="MySQL user name"; / /The password to connect to the MySQL database PASSWORD_MySQL="MySQL password" final String PASSWORD_MySQL="MySQL password"; connection=null; int count=0; try{ //load the driver Class.forName("com.mysql.jdbc.Driver" ).newInstance(); //Get the connection with the database connection= DriverManager.getConnection(URL_MySQL,NAME_MySQL,PASSWORD_MySQL); String sql="SELECT COUNT(*) FROM worker WHERE sex = 'female'";














statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
while (resultSet.next())
{
count=resultSet.getInt(1);
}
}catch (Exception e){
e.printStackTrace();
}
return count;
}

2. Employee information

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. User data operation class

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. Add function

add.png
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. Delete function

delete.png
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.setButtonListDe leteOnClickListener(new buttonListDeleteOnClickListener() { @Override public void buttonListDeleteOnClickListener(View view, int position) { final Worker worker=workerList.get(position); //Create the builder of the reminder dialog box AlertDialog.Builder builder=new AlertDialog.Builder(DeleteActivity.this); //Design dialog Box title icon builder.setIcon(R.mipmap.ic_launcher); //Set dialog box title text
















builder.setTitle("Dear user");
//Set the text of the dialog box
builder.setMessage("Are you sure you want to delete the employee whose employee number is "+worker.getId()+"?"); //
Set the dialog Positive button text of the box and its click listener
builder.setPositiveButton("OK", 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(workerBaseAdapter2
null)
{ workerBaseAdapter2 = New WorkerBaseAdapter2 (deleteActivity.this, workerlist); listView_all_Worker2.Setadapter (workerBaseAdapter2); } Els E { workerBaseAdapter2.setworkerList (workerlist); workerBaseAdapter2.notifyDataSetchanged (); } } ); } ). Start () ; } }); } }).start(); } }); builder.setNegativeButton("Cancel", null); AlertDialog alertDialog=builder.create();//Construct the alert dialog object alertDialog.show according to the builder ();//Display the alert dialog box //Design the AlertDialog alert dialog box size WindowManager.LayoutParams layoutParams=alertDialog.getWindow().getAttributes();






















layoutParams.width=700;
layoutParams.height=565;
alertDialog.getWindow().setAttributes(layoutParams);//Set the width and height of 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. Modification function

modify.png

7. Query function

query.png
Due to space reasons, I will not show more

Guess you like

Origin blog.csdn.net/m0_69101244/article/details/130792062