学生数据库管理APP设计目标:
- 作为教学用例,尽量简洁,容易让学生听懂。
- 所见即所得,增删改查后,及时在界面上显示。其他方式查看数据库的方法可以作为下一步进阶再学。
- 代码尽最大可能精简,注释多写一些。
- 暂不考虑性能,用最朴素最好理解的方法。
- 界面尽量少,最好就一个。
- 类的使用也不要过于繁杂,结构清晰一些。
- 功能要齐全
- 简单数据库,含一张表 Student(ID,Name,Age)
相关XML代码:
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 7 android:orientation="horizontal" 8 android:layout_width="match_parent" 9 android:layout_height="50dip" > 10 <Button 11 android:id="@+id/db_create" 12 android:layout_width="0dp" 13 android:layout_height="wrap_content" 14 android:layout_weight="1" 15 android:text="创建库和表" /> 16 <Button 17 android:id="@+id/db_init" 18 android:layout_width="0dp" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:text="初始化" /> 22 <Button 23 android:id="@+id/db_list" 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_weight="1" 27 android:text="显示内容" /> 28 </LinearLayout> 29 30 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 31 android:orientation="horizontal" 32 android:layout_width="match_parent" 33 android:layout_height="50dip" > 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="33dp" 37 android:text="学号:" 38 android:gravity="center" 39 android:layout_weight="1" /> 40 41 <EditText 42 android:id="@+id/stu_no" 43 android:layout_width="wrap_content" 44 android:layout_height="match_parent" 45 android:gravity="center" 46 android:layout_weight="2" 47 android:hint="int" 48 android:inputType="number" 49 android:maxLines="1" 50 android:text="" /> 51 <TextView 52 android:layout_width="wrap_content" 53 android:layout_height="33dp" 54 android:text="姓名:" 55 android:gravity="center" 56 android:layout_weight="1" /> 57 58 <EditText 59 android:id="@+id/stu_name" 60 android:layout_width="wrap_content" 61 android:layout_height="match_parent" 62 android:gravity="center" 63 android:layout_weight="2" 64 android:hint="string" 65 android:maxLines="1" 66 android:text="" /> 67 <TextView 68 android:layout_width="wrap_content" 69 android:layout_height="33dp" 70 android:gravity="center" 71 android:text="年龄:" 72 android:layout_weight="1"/> 73 74 <EditText 75 android:id="@+id/stu_age" 76 android:layout_width="wrap_content" 77 android:layout_height="match_parent" 78 android:layout_weight="2" 79 android:gravity="center" 80 android:hint="int" 81 android:inputType="number" 82 android:maxLines="1" 83 android:text="" /> 84 </LinearLayout> 85 86 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 87 android:orientation="horizontal" 88 android:layout_width="match_parent" 89 android:layout_height="50dip" > 90 <Button 91 android:id="@+id/db_insert" 92 android:layout_width="wrap_content" 93 android:layout_height="wrap_content" 94 android:layout_weight="1" 95 android:text="增加" /> 96 <Button 97 android:id="@+id/db_delete" 98 android:layout_width="wrap_content" 99 android:layout_height="wrap_content" 100 android:layout_weight="1" 101 android:text="删除" /> 102 <Button 103 android:id="@+id/db_update" 104 android:layout_width="wrap_content" 105 android:layout_height="wrap_content" 106 android:layout_weight="1" 107 android:text="修改" /> 108 <Button 109 android:id="@+id/db_query" 110 android:layout_width="wrap_content" 111 android:layout_height="wrap_content" 112 android:layout_weight="1" 113 android:text="查询" /> 114 </LinearLayout> 115 116 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 117 android:layout_width="match_parent" 118 android:layout_height="wrap_content" 119 android:background="#cccccc" 120 android:orientation="horizontal"> 121 <TextView 122 android:layout_width="wrap_content" 123 android:layout_height="33dp" 124 android:layout_weight="1" 125 android:gravity="center" 126 android:textColor="@color/colorPrimary" 127 android:text="学号" /> 128 129 <TextView 130 android:layout_width="wrap_content" 131 android:layout_height="33dp" 132 android:layout_weight="1" 133 android:gravity="center" 134 android:textColor="@color/colorPrimary" 135 android:text="姓名" /> 136 137 <TextView 138 android:layout_width="wrap_content" 139 android:layout_height="33dp" 140 android:layout_weight="1" 141 android:gravity="center" 142 android:textColor="@color/colorPrimary" 143 android:text="年龄" /> 144 </LinearLayout> 145 146 <android.support.v7.widget.RecyclerView 147 android:id="@+id/my_recycler_view" 148 android:layout_width="match_parent" 149 android:layout_height="match_parent" /> 150 151 </LinearLayout>
activity_stu.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:orientation="horizontal" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content"> 7 <TextView 8 android:id="@+id/item_stuID" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:gravity="center" 12 android:layout_weight="1" 13 tools:text="item"/> 14 <TextView 15 android:id="@+id/item_stuName" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:gravity="center" 19 android:layout_weight="1" 20 tools:text="item"/> 21 <TextView 22 android:id="@+id/item_stuAge" 23 android:layout_width="match_parent" 24 android:layout_height="match_parent" 25 android:layout_weight="1" 26 android:gravity="center" 27 tools:text="item"/> 28 </LinearLayout>
展示结果使用RecyclerView
Ref:http://www.cnblogs.com/hbuwyg/p/6978592.html
操作数据库方法
Ref:http://www.cnblogs.com/hbuwyg/p/7048857.html
Ref:http://www.cnblogs.com/hbuwyg/p/7045722.html