安卓最后一个大题复习

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

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


目录
相关文章
|
23天前
|
Web App开发 安全 程序员
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
多年的互联网寒冬在今年尤为凛冽,坚守安卓开发愈发不易。面对是否转行或学习新技术的迷茫,安卓程序员可从三个方向进阶:1)钻研谷歌新技术,如Kotlin、Flutter、Jetpack等;2)拓展新功能应用,掌握Socket、OpenGL、WebRTC等专业领域技能;3)结合其他行业,如汽车、游戏、安全等,拓宽职业道路。这三个方向各有学习难度和保饭碗指数,助你在安卓开发领域持续成长。
53 1
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
|
2月前
|
人工智能 Android开发 iOS开发
安卓与iOS开发:平台选择的艺术
在移动应用开发的广阔天地里,安卓和iOS两大操作系统各占半壁江山。本文将深入探讨这两个平台的开发环境、工具及市场趋势,帮助开发者在选择适合自己项目的平台时做出更明智的决策。通过比较各自的优势与局限,我们不仅能更好地理解每个系统的核心特性,还能洞察未来技术发展的脉络。无论你是刚入行的新手还是资深开发者,这篇文章都将为你提供有价值的参考和启示。
47 5
|
2月前
|
移动开发 IDE Android开发
安卓与iOS开发环境的深度剖析
本文将深入探讨安卓(Android)和iOS两大主流移动操作系统的开发环境,从技术架构、开发语言、工具链、生态系统等多个维度进行对比分析。我们将揭示两者在应用开发过程中的异同点,为开发者提供选择平台时的技术参考,同时展望移动开发领域的未来趋势。通过综合比较,本文旨在呈现一个全面、立体的开发环境画像,助力开发者更好地理解并适应不断变化的移动技术生态。
52 0
|
3月前
|
API Android开发 图形学
UNITY与安卓⭐三、安卓报错答疑合集
UNITY与安卓⭐三、安卓报错答疑合集
|
3月前
|
移动开发 Java Android开发
安卓与iOS开发:异同探析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自占据半壁江山。本文旨在深入探讨这两个平台在开发环境、编程语言、用户界面设计、性能优化及市场分布等方面的异同,为开发者提供实用的比较视角和决策参考。通过对比分析,我们不仅能更清晰地认识到各平台的特性,还能洞察未来移动开发的可能趋势。
|
3月前
|
Java 图形学 Android开发
UNITY与安卓⭐二、AndroidStudio中关于通讯的使用教学
UNITY与安卓⭐二、AndroidStudio中关于通讯的使用教学
|
3月前
|
搜索推荐 Android开发 iOS开发
探索安卓与iOS开发的差异之美
在数字时代的浪潮中,移动应用开发如同一场精心编排的交响乐,安卓和iOS这两大平台扮演着不同乐器的角色,各自以独特的方式奏响。本文将带领读者走进这场音乐盛宴,感受两大平台在开发过程中所展现的不同韵律,从设计理念到用户体验,从市场占有率到生态系统,我们将一探究竟,欣赏它们如何在竞争激烈的市场中和谐共存,共同推动技术的进步与创新。
42 0
|
5月前
|
存储 程序员 定位技术
程序员必知:安卓的四大组件
程序员必知:安卓的四大组件
39 0
|
网络协议 Java Shell
学习安卓过程中踩的坑
学习安卓过程中踩的坑
|
安全 IDE Java
安卓官网使用小知识串讲
打开安卓开发者官网,映入眼帘的就是三个词“Android Studio”、“Google Play”和“Jetpack”