安卓最后一个大题复习

简介: 安卓最后一个大题复习

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) {
    }
}


目录
相关文章
|
5月前
|
人工智能 Android开发 iOS开发
安卓与iOS开发:平台选择的艺术
在移动应用开发的广阔天地里,安卓和iOS两大操作系统各占半壁江山。本文将深入探讨这两个平台的开发环境、工具及市场趋势,帮助开发者在选择适合自己项目的平台时做出更明智的决策。通过比较各自的优势与局限,我们不仅能更好地理解每个系统的核心特性,还能洞察未来技术发展的脉络。无论你是刚入行的新手还是资深开发者,这篇文章都将为你提供有价值的参考和启示。
78 5
|
6月前
|
API Android开发 图形学
UNITY与安卓⭐三、安卓报错答疑合集
UNITY与安卓⭐三、安卓报错答疑合集
|
8月前
|
网络协议 算法 安全
小米安卓春招面试一面
小米安卓春招面试一面
61 3
|
8月前
|
存储 程序员 定位技术
程序员必知:安卓的四大组件
程序员必知:安卓的四大组件
98 0
|
网络协议 Java Shell
学习安卓过程中踩的坑
学习安卓过程中踩的坑
|
安全 IDE Java
安卓官网使用小知识串讲
打开安卓开发者官网,映入眼帘的就是三个词“Android Studio”、“Google Play”和“Jetpack”
110 0
|
传感器 安全 测试技术
安卓13又来了?快!扶起我来!
一年一年过的太快了,还记得两年前写了 Android 11(R) 的适配文章,这一转眼都13(T)了,今天我们一起来学习适配下 Android 13 吧!
281 0
安卓13又来了?快!扶起我来!
|
存储 缓存 安全
安卓11?快!扶我起来继续学
安卓11更新的主要内容
202 0
|
数据管理 API Android开发
|
安全 Java Android开发
浅谈安卓apk加固原理和实现
在安卓开发中,打包发布是开发的最后一个环节,apk是整个项目的源码和资源的结合体;对于懂点反编译原理的人可以轻松编译出apk的源码资源,并且可以修改资源代码、重新打包编译,轻轻松松变成自己的apk或者修改其中一部分窃取用户信息。
4907 1

热门文章

最新文章