最近一段时间完善了人脸识别考勤系统,做得大部分功能都比较简单,这里写一些笔记做一下记录。期间参考资料太多……也不记得参考过哪些博文,有些比较好的笔记,后期再贴出来
先写一下最基础的功能,对员工的增删改查操作。
先看一下界面,后面贴上主要代码,布局就不贴啦
后面等项目都完成,再上传整个项目源码
员工实体类
EmployeeInfo.class
/*
*@Author:Swallow
*@Date:2019/2/22
* 存放员工
*/
public class EmployeeInfo {
private String emid;
private String emname;
private String imgurl;
public EmployeeInfo() {
}
public EmployeeInfo(String emid, String emname, String imgurl) {
this.emid = emid;
this.emname = emname;
this.imgurl = imgurl;
}
public String getEmid() {
return emid;
}
public void setEmid(String emid) {
this.emid = emid;
}
public String getEmname() {
return emname;
}
public void setEmname(String emname) {
this.emname = emname;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
@Override
public String toString() {
return "EmployeeInfo{" +
"emid=" + emid +
", emname='" + emname + '\'' +
", imgurl='" + imgurl + '\'' +
'}';
}
}
有做数据存储的,我习惯写一个类存放常用字段,这样平时写不容易写错
EmployeeTable.class
/*
*@Author:Swallow
*@Date:2019/2/22
*/
public class EmployeeTable {
public static final String TAB_NAME = "tab_employee";
public static final String EM_ID = "emid";
public static final String EM_NAME = "emname";
public static final String EM_URL = "imgurl";
public static final String CREATE_TAB = "create table if not exists "
+ TAB_NAME + " ("
+ EM_ID + " VARCHAR(255) primary key,"
+ EM_NAME + " VARCHAR(255),"
+ EM_URL + " VARCHAR(255));";
}
然后是操作处理用到的DAO类,这个比较重要,方法封装进来,直接调用就行
EmployeeDao.class
/*
*@Author:Swallow
*@Date:2019/2/25
*/
public class EmployeeDao {
private DBHelper dbHelper;
private SQLiteDatabase sqliteDatabase;
public EmployeeDao(Context context) {
dbHelper = new DBHelper(context,"Data.db",null,1);
}
//根据工号查询
public EmployeeInfo findemployeebyid(String emid){
sqliteDatabase = dbHelper.getWritableDatabase();
String sql = "select * from " + EmployeeTable.TAB_NAME + " where emid=?";
String[] selectionArgs = new String[] { emid };
Cursor cursor = sqliteDatabase.rawQuery(sql, selectionArgs);
if(cursor.moveToNext())// 判断Cursor中是否有数据
{
EmployeeInfo employee = new EmployeeInfo();
employee.setEmid(cursor.getString(cursor.getColumnIndex(EmployeeTable.EM_ID)));
employee.setEmname(cursor.getString(cursor.getColumnIndex(EmployeeTable.EM_NAME)));
employee.setImgurl(cursor.getString(cursor.getColumnIndex(EmployeeTable.EM_URL)));
Log.v("imgpath>>>>>>",employee.getImgurl());
return employee;
}
return null;
}
//添加员工信息
public void addEmployee(String emid, String emname, String imgurl) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(EmployeeTable.EM_ID,emid);
values.put(EmployeeTable.EM_NAME,emname);
values.put(EmployeeTable.EM_URL,imgurl);
db.insert(EmployeeTable.TAB_NAME,null,values);
db.close();
}
//根据员工编号检验员工是否存在
public boolean CheckIsDataAlreadyInDBorNot(String emid) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String Query = "Select * from " + EmployeeTable.TAB_NAME + " where emid =?";
Cursor cursor = db.rawQuery(Query, new String[]{emid});
if (cursor.getCount() > 0) {
cursor.close();
return true;
}
cursor.close();
return false;
}
//下面两个方法返回一个ArrayList>对象,是为了方便调用的时候数据传给listView进行展示
//根据输入的员工工号进行查找,查找单个员工
private ArrayList> findemployeebyid(String getemid) {
//String etemid = et_findbyemid.getText().toString();//获取输入的员工工号
ArrayList> employees = new ArrayList>();
SQLiteDatabase db = dbHelper.getReadableDatabase();//设置数据库为可读
String findsql = "select * from " + EmployeeTable.TAB_NAME + " where emid=?";
Cursor cursor = db.rawQuery(findsql,new String[]{getemid});
if(cursor.moveToNext())// 判断Cursor中是否有数据
{
String emid = cursor.getString(0);
String emname = cursor.getString(1);
HashMap hash = new HashMap();
hash.put("emid",emid);
hash.put("emname",emname);
employees.add(hash);
}else {
return null;
}
return employees;
}
//查询所有员工
private ArrayList> Getemployees(String sql) {
ArrayList> employeelist = new ArrayList>();
ArrayList employeeInfos = new ArrayList<>();
//dbHelper = new DBHelper(this,"Data.db",null,1);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(sql,null);
while (cursor.moveToNext()){
HashMap hash = new HashMap();
//读取数据并且进行封装
String emid = cursor.getString(0);
String emname = cursor.getString(1);
hash.put("emid",emid);
hash.put("emname",emname);
employeelist.add(hash);
Log.v("list>>>>>",emid + emname);
}
return employeelist;
}
//删除员工
public void deleteEmployee(String emid){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "delete from " + EmployeeTable.TAB_NAME + " where emid=?";
db.execSQL(sql,new String[]{emid});
}
//修改员工信息,这里我设定的是工号不可修改,根据工号修改员工的姓名和照片
public void updataEmployee(String id,String name,String path){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(EmployeeTable.EM_URL, path);
values.put(EmployeeTable.EM_NAME, name);
db.update(EmployeeTable.TAB_NAME, values,"emid='" + id +"'",null);
//id是String类型,设置条件句要用 ' ' 包围
db.close();
}
}
另外,当时有一个坑,挺坑的
就是传入的sql语句一定要注意写得全不全,之前我就是少了个单引号折腾很久
一开始看别人写的传入的String类型没有单引号包围,但实际上是要写的,如果是int类型就可以不用
展示员工列表界面的Activity类
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.taixueface.bean.EmployeeInfo;
import com.taixueface.dao.EmployeeDao;
import com.taixueface.dao.EmployeeTable;
import java.util.ArrayList;
import java.util.HashMap;
public class ManageActivity extends Activity implements View.OnClickListener {
private EditText et_findbyemid;
private ImageButton btn_findbyemid;
private Button btn_record,btn_addem,btn_set,btn_back;
private LinearLayout ly_manage;
private ListView lv_employee;
private EmployeeDao employeeDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage);
init();
String selectsql = "select * from " + EmployeeTable.TAB_NAME + ";";
showlist(selectsql);
//设置点击List当中的某一项跳转到新界面的监听
//新界面为该员工的详细信息界面MessageActivity
lv_employee.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int arg0, long arg1) {
Log.v("arg0>>>>>>", String.valueOf(arg0));
HashMap hashMap = (HashMap) adapterView.getItemAtPosition(arg0);
final String emid = hashMap.get(EmployeeTable.EM_ID).toString() ;
Log.v("click get>>>>>>>>> ", emid);
//获取点击信息传递到下一个页面
Bundle bundle = new Bundle();
bundle.putString(EmployeeTable.EM_ID,emid);
Intent msg_intent = new Intent();
msg_intent.putExtras(bundle);
msg_intent.setClass(ManageActivity.this,MessageActivity.class);
startActivity(msg_intent);
}
});
lv_employee.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> arg0, View arg1, int arg2, final long arg3) {
//长按弹出提示框进行删除数据操作
HashMap hm = (HashMap) arg0.getItemAtPosition(arg2);
final String emid = hm.get("emid").toString();// 长按时获取id,赋值给id
Log.v("longgetemid>>>>>>>>> ", emid);
AlertDialog.Builder builder=new AlertDialog.Builder(ManageActivity.this);
builder.setMessage("是否删除该员工信息?");
builder.setTitle("提示");
//设置确定按钮的监听器,以下为删除操作
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
employeeDao.deleteEmployee(emid);
String afteraql = "select * from " + EmployeeTable.TAB_NAME + ";";
showlist(afteraql);
//Log.v("id>>>>>",i+ "long" + l);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
return false;
}
});
}
//初始化控件
private void init() {
et_findbyemid = findViewById(R.id.et_findbyemid);
btn_findbyemid = findViewById(R.id.btn_findbyemid);
btn_record = findViewById(R.id.btn_record);
btn_addem = findViewById(R.id.btn_addem);
btn_set = findViewById(R.id.btn_set);
btn_back = findViewById(R.id.btn_back);
lv_employee = findViewById(R.id.lv_employee);
ly_manage = findViewById(R.id.ly_manage);
ly_manage.setOnClickListener(this);
btn_findbyemid.setOnClickListener(this);
btn_record.setOnClickListener(this);
btn_addem.setOnClickListener(this);
btn_set.setOnClickListener(this);
btn_back.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_back:
Intent back_intent = new Intent(this,MainActivity.class);
startActivity(back_intent);
break;
case R.id.ly_manage:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
break;
case R.id.btn_findbyemid:
checkEmployee();
break;
case R.id.btn_record:
Intent record_intent = new Intent(ManageActivity.this,
RecordActivity.class);
startActivity(record_intent);
break;
case R.id.btn_addem:
Intent add_intent = new Intent(ManageActivity.this,
AddActivity.class);
startActivity(add_intent);
break;
case R.id.btn_set:
Intent set_intent = new Intent(ManageActivity.this,
SetActivity.class);
startActivity(set_intent);
break;
}
}
//根据输入的员工工号进行查找
private void checkEmployee() {
String etemid = et_findbyemid.getText().toString();//获取输入的员工工号
if(!etemid.isEmpty()){
ArrayList> employees = employeeDao.findemployeebyid(etemid);
if(!employees.isEmpty())// 判断是否有数据
{
//显示数据
SimpleAdapter lv_ems = new SimpleAdapter(this,
employees,
R.layout.employeelist,
new String[] { "emid", "emname" },
new int[]{R.id.tv_emid,R.id.tv_emname});
lv_employee.setAdapter(lv_ems);
}else {
Toast.makeText(this,"该员工不存在",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this,"请输入员工编号",Toast.LENGTH_SHORT).show();
}
}
//显示数据
public void showlist(String sql){
ArrayList> employeelist = employeeDao.Getemployees(sql);
//ListView适配器
SimpleAdapter lv_ems = new SimpleAdapter(this,
employeelist,
R.layout.employeelist,
new String[] { "emid", "emname" },
new int[]{R.id.tv_emid,R.id.tv_emname});
lv_employee.setAdapter(lv_ems);
}
}
添加员工,这里先写一下添加按钮的监听方法
case R.id.btn_addem:
String emid = dt_emid.getText().toString();
String emname = et_emname.getText().toString();
String imgurl = url;
Log.v("add>>>",emid + emname + imgurl);
if (employeeDao.CheckIsDataAlreadyInDBorNot(emid)) {
Toast.makeText(this,
"该工号已存在,添加失败", Toast.LENGTH_SHORT).show();
}else {
if((!emid.trim().equals(""))
&& (!emname.trim().equals(""))
&& (urlmsg == MsgTable.SUCCESS)){
employeeDao.addEmployee(emid,emname,imgurl);
Toast.makeText(this,
"添加员工成功!", Toast.LENGTH_SHORT).show();
Message message = new Message();
message.what = MsgTable.REFRESH;
handler.sendMessage(message);
}else {
//switch (v.getId())
Toast.makeText(this,
"信息不能为空", Toast.LENGTH_SHORT).show();
}
}
break;