teacher_layout.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="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="20dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="教师号:" android:textSize="20dp" /> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/teacherID"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="姓 名:" android:textSize="20dp"/> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/teacherName"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="性 别:" android:textSize="20dp"/> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/teacherSex"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="工 资:" android:textSize="20dp"/> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/teacherWage"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" android:onClick="add"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:onClick="del"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" android:onClick="update"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:onClick="select"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ListView android:id="@+id/listViewStudent" android:layout_width="390dp" android:layout_height="362dp" android:dividerHeight="1dp" android:layout_margin="15dp" android:divider="@color/colorPrimary"> </ListView> </LinearLayout> </LinearLayout>
student_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item_teacherID" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="2"/> <TextView android:id="@+id/item_teacherName" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> <TextView android:id="@+id/item_teacherSex" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> <TextView android:id="@+id/item_teacherWage" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> </LinearLayout>
student_item_header.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 设控件的宽度为0,然后设置layout_weight,让他们按比例瓜分整个这一行的宽度,从而保证每一行的各列宽度都一样 --> <TextView android:text="教师号" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="2"/> <TextView android:text="姓 名" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> <TextView android:text="性 别" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> <TextView android:text="工资" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:layout_weight="1"/> </LinearLayout>
TeacherActivity.java
public class TeacherActivity extends Activity { EditText teacherID, //教师号 teacherName,//姓 名 teacherSex, //性 别 teacherWage;//工资 ListView studentListView;//数据显示表格 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.teacher_layout); //初始化组件 initModule(); //查询数据 selectAll(); } //添加 public void add(View view){ Teacher th = new Teacher(teacherID.getText().toString(),teacherName.getText().toString(),teacherSex.getText().toString(),Integer.parseInt(teacherWage.getText().toString())); TeacherDao.add(TeacherActivity.this, th); selectAll(); } //删除 public void del(View view){ Teacher th = new Teacher(); String teacherID_str = teacherID.getText().toString(); String teacherName_str = teacherName.getText().toString(); String teacherSex_str = teacherSex.getText().toString(); th.setTeacherID(teacherID_str==null?"":teacherID_str); th.setTeacherName(teacherName_str==null?"":teacherName_str); th.setTeacherSex(teacherSex_str==null?"":teacherSex_str); TeacherDao.deleteTeacherInfo(TeacherActivity.this,th); selectAll(); } //修改 public void update(View view){ Teacher th = new Teacher(); String teacherID_str = teacherID.getText().toString(); String teacherName_str = teacherName.getText().toString(); String teacherSex_str = teacherSex.getText().toString(); String teacherWage_str = teacherWage.getText().toString(); th.setTeacherID((teacherID_str==null || teacherID_str.equals(""))?"":teacherID_str); th.setTeacherName((teacherName_str==null || teacherName_str.equals(""))?"":teacherName_str); th.setTeacherSex((teacherSex_str==null || teacherSex_str.equals(""))?"":teacherSex_str); th.setTeacherWage((teacherWage_str==null || teacherWage_str.equals(""))?-10:Integer.parseInt(teacherWage_str)); TeacherDao.update(TeacherActivity.this,th); selectAll(); } //查询 public void select(View view){ Teacher th = new Teacher(); String teacherID_str = teacherID.getText().toString(); String teacherName_str = teacherName.getText().toString(); String teacherSex_str = teacherSex.getText().toString(); String teacherWage_str = teacherWage.getText().toString(); th.setTeacherID((teacherID_str==null || teacherID_str.equals(""))?"":teacherID_str); th.setTeacherName((teacherName_str==null || teacherName_str.equals(""))?"":teacherName_str); th.setTeacherSex((teacherSex_str==null || teacherSex_str.equals(""))?"":teacherSex_str); th.setTeacherWage((teacherWage_str==null || teacherWage_str.equals(""))?-10:Integer.parseInt(teacherWage_str)); selectAll(th); } //查 public void selectAll(){ selectAll(new Teacher()); } public void selectAll(Teacher th){ List<Teacher> teacherAllInfo = TeacherDao.getAll(TeacherActivity.this,th); //获取到集合数据 List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); for (int i = 0;i<teacherAllInfo.size();i++){ HashMap<String, Object> item = new HashMap<String, Object>(); Teacher teacher = teacherAllInfo.get(i); item.put("item_teacherID", teacher.getTeacherID()); item.put("item_teacherName", teacher.getTeacherName()); item.put("item_teacherSex", teacher.getTeacherSex()); item.put("item_teacherWage", teacher.getTeacherWage()); data.add(item); } //创建SimpleAdapter适配器将数据绑定到item显示控件上 SimpleAdapter adapter = new SimpleAdapter(TeacherActivity.this, data, R.layout.student_item, new String[]{"item_teacherID", "item_teacherName", "item_teacherSex","item_teacherWage"}, new int[]{R.id.item_teacherID, R.id.item_teacherName, R.id.item_teacherSex,R.id.item_teacherWage}); //实现列表的显示 studentListView.setAdapter(adapter); } //初始化组件 public void initModule(){ teacherID = findViewById(R.id.teacherID); //教师号 teacherName = findViewById(R.id.teacherName);//姓 名 teacherSex = findViewById(R.id.teacherSex); //性 别 teacherWage = findViewById(R.id.teacherWage);//工资 studentListView = findViewById(R.id.listViewStudent);//显示表格 View view = LayoutInflater.from(this).inflate(R.layout.student_item_header,null); studentListView.addHeaderView(view); } }
TeacherDao
/** * 操作教师数据库 */ public class TeacherDao { //插入教师信息 public static void add(Context context, Teacher teacher){ //创建或打开数据库 SQLiteDatabase db = getDB(context); String addSql = "insert into teacher(teacherID,teacherName,teacherSex,teacherWage) values(?,?,?,?)"; db.execSQL(addSql,new Object[]{teacher.getTeacherID(),teacher.getTeacherName(),teacher.getTeacherSex(),teacher.getTeacherWage()}); db.close(); } //查询所有的教师信息 public static List<Teacher> getAll(Context context,Teacher teacher){ List<Teacher> list = new ArrayList<>(); SQLiteDatabase db = getDB(context); String sql = "select teacherID,teacherName,teacherSex,teacherWage from teacher where 1=1 "; if(teacher.getTeacherID()!=null && !teacher.getTeacherID().equals("")){ sql = sql + " and teacherID like '%"+teacher.getTeacherID()+"%'"; } if(teacher.getTeacherName()!=null && !teacher.getTeacherName().equals("")){ sql = sql + " and teacherName like '%"+teacher.getTeacherName()+"%'"; } if(teacher.getTeacherSex()!=null && !teacher.getTeacherSex().equals("")){ sql = sql + " and teacherSex='"+teacher.getTeacherSex()+"'"; } if(teacher.getTeacherWage()>0){ sql = sql + " and teacherWage='"+teacher.getTeacherWage()+"'"; } Cursor cursor = db.rawQuery(sql, null); while (cursor.moveToNext()){ Teacher th = new Teacher(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3)); list.add(th); } db.close(); return list; } //创建或打开数据库 public static SQLiteDatabase getDB(Context context){ DatabaseHelper dbher = new DatabaseHelper(context,"th",null,1); SQLiteDatabase db = dbher.getReadableDatabase(); return db; } //删除数据 public static void deleteTeacherInfo(Context context, Teacher teacher) { SQLiteDatabase db = getDB(context); String sql = "delete from teacher where 1=1 "; if(teacher.getTeacherID()!=null && !teacher.getTeacherID().equals("")){ sql = sql + " and teacherID='"+teacher.getTeacherID()+"'"; } if(teacher.getTeacherName()!=null && !teacher.getTeacherName().equals("")){ sql = sql + " and teacherName='"+teacher.getTeacherName()+"'"; } if(teacher.getTeacherSex()!=null && !teacher.getTeacherSex().equals("")){ sql = sql + " and teacherSex='"+teacher.getTeacherSex()+"'"; } db.execSQL(sql); db.close(); } //修改 public static void update(Context context, Teacher teacher) { SQLiteDatabase db = getDB(context); String sql = "update teacher set teacherName=?,teacherSex=?,teacherWage=? where teacherID=?"; db.execSQL(sql,new Object[]{teacher.getTeacherName(),teacher.getTeacherSex(),teacher.getTeacherWage(),teacher.getTeacherID()}); db.close(); } }
Teacher
/** * 教师实体类 */ public class Teacher { String teacherID;//教师号 String teacherName;//姓 名 String teacherSex; //性 别 int teacherWage;//工资 String tableId;//数据显示区 public Teacher() { } public Teacher(String teacherID, String teacherName, String teacherSex, int teacherWage) { this.teacherID = teacherID; this.teacherName = teacherName; this.teacherSex = teacherSex; this.teacherWage = teacherWage; } public String getTeacherID() { return teacherID; } public void setTeacherID(String teacherID) { this.teacherID = teacherID; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public String getTeacherSex() { return teacherSex; } public void setTeacherSex(String teacherSex) { this.teacherSex = teacherSex; } public int getTeacherWage() { return teacherWage; } public void setTeacherWage(int teacherWage) { this.teacherWage = teacherWage; } public String getTableId() { return tableId; } public void setTableId(String tableId) { this.tableId = tableId; } }
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, "zh.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { //执行sql语句创建数据库 String teacherSql = "create table teacher(teacherID text primary key,teacherName text,teacherSex text,teacherWage int)"; db.execSQL(teacherSql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }