Personnel management system APP developed based on Android Studio

personnel management system


foreword

This is a personnel management system with a login function and personnel information addition, deletion, modification and query functions. I have also made an app that connects to the cloud platform through the http protocol before. It just needs to complete a course design, so I will take a good tutorial on Android development. knowledge.

1. The general flow of the system

2. Detailed development steps

1. Login interface

The UI effect is as shown in the figure:

This interface does not use a database and can be added later. The specific code is as follows:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username;
    private EditText pwd;
    private Button login;
    private String Tag="QQLoginActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username=findViewById(R.id.user_name);
        pwd=findViewById(R.id.etpwd);
        login=findViewById(R.id.btnLogin);


        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btnLogin: //这里是登录按钮的功能实现
                String userName=username.getText().toString();
                String user_pwd=pwd.getText().toString();
                if (TextUtils.isEmpty(userName))
                {
                    Toast.makeText(LoginActivity.this,"用户名为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(user_pwd))
                {
                    Toast.makeText(LoginActivity.this,"密码为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(login(userName,user_pwd))
                {
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(LoginActivity.this,CenterActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    private boolean login(String userName,String pwd)
    {
        if(userName.equals("admin")&&pwd.equals("123"))
        {

            return true;
        }else{

            return false;
        }

    }
}

2. Intermediate jump interface

The design of this interface is relatively simple. There is only one picture and two picture buttons, so I won’t introduce it in detail. The specific effect is as follows:

3. Add user

Here we need to use the increase statement of the database. First of all, we need to create a database and add a table.

Create a DbOpenHelper class that inherits from the SQLiteOpenHelper class.

Override the onCreate and onUpgrade methods of the SQLiteOpenHelper class.

public class DbOpenHelper extends SQLiteOpenHelper {

    public DbOpenHelper(@Nullable Context context) {
        super(context, "my.db3", null, 1);

    }


    //必须重写下面两个方法
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        //当没有的时候新建一个数据库
        sqLiteDatabase.execSQL("CREATE TABLE student(id integer primary key autoincrement,name varchar,age int, sex int,mz varchar,xy varchar,zy varchar,bj carchar)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

Implement the database increase function in the studentdao class:

 public void insert(StudentEntity entity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into student(name,age,sex,mz ,xy ,zy ,bj ) values(?,?,?,?,?,?,?)";
        //拼接sql时选择的是防止注入的方式
        db.execSQL(sql, new String[]{entity.getName(), String.valueOf(entity.getAge()), String.valueOf(entity.getSex()),
                entity.getMz(), entity.getXy(), entity.getZy(), entity.getBj()});
        db.close();
    }

Let the user fill in the relevant information on the UI interface and then store the information in the database, as shown in the following figure:

The implementation code is as follows:

public class AddActivity extends AppCompatActivity implements View.OnClickListener {
    private RadioGroup radioGroup;
    private EditText etname;
    private EditText etage;
    private EditText etmz;
    private EditText etxy;
    private EditText etzy;
    private EditText etbj;
    private int sex;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        etname = findViewById(R.id.etname);
        etage = findViewById(R.id.etage);
        etmz = findViewById(R.id.etmz);
        etxy = findViewById(R.id.etxy);
        etzy = findViewById(R.id.etzy);
        etbj = findViewById(R.id.etbj);
        radioGroup=findViewById(R.id.rg);


        findViewById(R.id.btnadd).setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.rgnan)
                {
                    sex=1;
                }else if (i==R.id.rgnv)
                {
                    sex=0;
                }
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnadd:
                String name = etname.getText().toString();
                String strage = etage.getText().toString();
                String mz = etmz.getText().toString();
                String xy = etxy.getText().toString();
                String zy = etzy.getText().toString();
                String bj = etbj.getText().toString();


                if (TextUtils.isEmpty(name)) {
                    return;
                }
                if (TextUtils.isEmpty(strage)) {
                    return;
                }
                if (TextUtils.isEmpty(mz)) {
                    return;
                }
                if (TextUtils.isEmpty(xy)) {
                    return;
                }
                if (TextUtils.isEmpty(zy)) {
                    return;
                }
                if (TextUtils.isEmpty(bj)) {
                    return;
                }


                int age = Integer.parseInt(strage);


                StudentEntity entity = new StudentEntity();
                entity.setAge(age);
                entity.setName(name);
                entity.setSex(sex);
                Log.i("sex", String.valueOf(sex));
                entity.setMz(mz);
                entity.setXy(xy);
                entity.setZy(zy);
                entity.setBj(bj);
                new StudentDao(this).insert(entity);
                Toast.makeText(AddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                break;


        }
    }

}

4. Full UI

        This includes delete, modify and query functions. The UI interface is as follows:

It is recommended to remove the query button in the red box. You can directly enter and display all the information. Here a listview is used to display all the information, so you must first load its data. I won’t introduce it here in detail. Of course, query, modify and delete functions It is necessary to continue to implement these three functions in studentdao.

The specific code of studentdao is as follows:

public class StudentDao {
    //负责对表增删改查
    DbOpenHelper helper;

    public StudentDao(Context context) {
        helper = new DbOpenHelper(context);
    }

    //1.增
    public void insert(StudentEntity entity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into student(name,age,sex,mz ,xy ,zy ,bj ) values(?,?,?,?,?,?,?)";
        //拼接sql时选择的是防止注入的方式
        db.execSQL(sql, new String[]{entity.getName(), String.valueOf(entity.getAge()), String.valueOf(entity.getSex()),
                entity.getMz(), entity.getXy(), entity.getZy(), entity.getBj()});
        db.close();
    }

    //2.删
    public void delete(int id) {

        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "delete from student where id=?";
        //拼接sql时选择的是防止注入的方式
        db.execSQL(sql, new String[]{id + ""});

        db.close();
    }

    //3.改
    public void update(StudentEntity entity) {

        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "update student set name=? , age=? ,sex=?,mz=? ,xy =?,zy =?,bj=? where id=?";
        //拼接sql时选择的是防止注入的方式
        db.execSQL(sql, new String[]{entity.getName(), entity.getAge() + "", String.valueOf(entity.getSex()),
                entity.getMz(), entity.getXy(), entity.getZy(), entity.getBj(), entity.getId() + ""});
        db.close();
    }


    //4.1 查询单个对象
    public StudentEntity get(int i) {
        StudentEntity entity = null;

        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "select * from student where id=?";

        Cursor cursor = db.rawQuery(sql, new String[]{i + ""});

        if (cursor != null) {
            if (cursor.moveToNext()) {
                entity = new StudentEntity();


                int index = cursor.getColumnIndex("name");
                String name = cursor.getString(index);

                index = cursor.getColumnIndex("age");
                int age = cursor.getInt(index);

                index = cursor.getColumnIndex("sex");
                int sex = cursor.getInt(index);

                index = cursor.getColumnIndex("mz");
                String mz = cursor.getString(index);

                index = cursor.getColumnIndex("xy");
                String xy = cursor.getString(index);

                index = cursor.getColumnIndex("zy");
                String zy = cursor.getString(index);

                index = cursor.getColumnIndex("bj");
                int bj = cursor.getInt(index);

                entity.setName(name);
                entity.setAge(age);
                entity.setSex(sex);
                entity.setMz(mz);
                entity.setXy(xy);
                entity.setZy(zy);
                entity.setBj(String.valueOf(bj));
            }
        }


        return entity;
    }

    //4.2
    public List<StudentEntity> getAll() {
        List<StudentEntity> list = new ArrayList<>();
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "select * from student";
        Cursor cursor = db.rawQuery(sql, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                int index = cursor.getColumnIndex("id");
                int id = cursor.getInt(index);

                index = cursor.getColumnIndex("name");
                String name = cursor.getString(index);

                index = cursor.getColumnIndex("age");
                int age = cursor.getInt(index);

                StudentEntity entity = new StudentEntity();
                entity.setName(name);
                entity.setAge(age);
                entity.setId(id);

                list.add(entity);

            }


            cursor.close();
            db.close();


        }


        return list;
    }


}

 The implementation code of all user functions is as follows:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView lvstudent;
    private BaseAdapter adapter;

    private List<StudentEntity> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvstudent = findViewById(R.id.lvstudent);


        findViewById(R.id.btnselect).setOnClickListener(this);


        adapter = new MyAdapter();
        lvstudent.setAdapter(adapter);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            if (view == null) {
                v = View.inflate(MainActivity.this, R.layout.list_view_item_students, null);
            } else {
                v = view;
            }

            StudentEntity entity = list.get(i);


            TextView tvName = v.findViewById(R.id.tvname);
            tvName.setText(entity.getName());

            TextView tvage = v.findViewById(R.id.tvage);
            //Log.i("id", valueOf(entity.getId()));

            tvage.setText(entity.getAge() + "");

            TextView tvid = v.findViewById(R.id.tvid);
            //Log.i("age", valueOf(entity.getAge()));

            tvid.setText(entity.getId() + "");


            v.findViewById(R.id.ibdetail).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("position", "position" + i);
                    StudentEntity studentEntity = list.get(i);

                    int id = studentEntity.getId();
                    Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                    intent.putExtra("", id);
                    MainActivity.this.startActivity(intent);
                }
            });
            v.findViewById(R.id.ibdelete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    StudentEntity studentEntity = list.get(i);

                    new StudentDao(MainActivity.this).delete(studentEntity.getId());

                    list.remove(i);
                    Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    adapter.notifyDataSetChanged();
                }
            });
            v.findViewById(R.id.ibmodify).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("position", "position" + i);
                    StudentEntity studentEntity = list.get(i);

                    int id = studentEntity.getId();
                    Intent intent = new Intent(MainActivity.this, ModifyActivity.class);
                    intent.putExtra("", id);
                    MainActivity.this.startActivity(intent);
                }
            });
            return v;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.btnselect:
                List<StudentEntity> _list = new StudentDao(this).getAll();
                list.clear();
                list.addAll(_list);
                adapter.notifyDataSetChanged();

                break;
        }
    }
}

 5. Project download

Some specific functions can’t be explained clearly one by one, you can download the complete project through the link below

Personnel management system developed based on Android studio

 

Summarize

This is a simple course design project, which is enough for non-computer or mobile development professional partners. There are more and more technologies, all we can do is keep improving and keep learning new technologies, come on.

Guess you like

Origin blog.csdn.net/weixin_46155589/article/details/127324961