转载请注明出处:明桑Android
这是作为上一篇Android 数据存储 如何搞定SQLite Database的实例练习,之所以单独列出来是因为除了数据库方面的知识,还涉及其它方面的知识,所以就写的详细点,啰嗦点。希望对初学者有所帮助。当然这个Demo比较简单,有很多可以改进的地方,但那不是这里探讨的重点,重点学习如何将SQLiteDatabase数据绑定到我们的界面!
本文代码地址:UseSQLitDatabase
我们要做一个简单的学生管理的demo,创建student.db,包括name,grade字段,实现增、删、改、查的功能;
实现效果:
1,界面分析
我们先不忙着码代码,先来看看这样一个简单的Demo**基本的界面结构**:
- 主界面是一个ListView用来显示学生信息,底部有两个Button,用来添加和查找学生信息;
- 点击**“添加”**Button,进入添加界面,输入姓名,分数,添加成功后返回主界面;
- 点击**“查找”**Button,弹出查找对话框,查找对话框此时包含一个EditView用来输入学生姓名和一个“查找”Button,当点击查找Button后,显示查找结果,如果查找结果为空,提示无此信息,如果不为空,点击结果可进入详细界面。
- 点击ListView Item可查看学生详细信息,在详细信息界面可对学生信息进行编辑修改和删除。
2,需要的的知识点:
- 自定义View的AlertDialog:因为我们的查找对话框需要我们自定义视图,不熟悉的请看Android常用实例—Alert Dialog的使用;
- 自定义Adapter的ListView:用来展示学生信息的ListView需要我们自定义Adapter将数据库中的数据填充到ListView。
- Toast用来提示消息:这个比较简单,用来提示一些信息,包括添加成功、删除成功等,Android消息提示:AlertDialog、Toast、Notification的使用;
- Activity之间的信息传递:我们需要将学生信息从一个Activity传递到另一个Activity,比如查看学生信息,或者添加成功后返回主界面,涉及到
Intent
和startActivityForResult()
等的使用。 - SQLiteDatabase的基本使用:这也是我们这个练习的重点,不熟悉的建议先看上篇文章Android 数据存储 如何搞定SQLite Database;
3,具体实现
创建项目后,根据上一篇文章所说,我们按照以下步骤来完成我们的Demo:
1,界面布局
通过界面分析我们可以看出:MainActivity用来显示所有学生列表信息,其他界面提供查看或者编辑修改以及删除的功能,在界面布局上非常相似,所以我们将它们放到同一个布局里面,只不过根据具体的情况,设置个某些控件的Visibility
属性为GONE
或者VISIBLE
;
主界面布局:res/layout/activity_main.xml
,包含一个ListView和两个Button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/stduent_list"
android:layout_alignParentTop="true"
android:divider="#5ABC4F"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_marginTop="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_add"
android:text="添 加"
android:textSize="25sp"
android:textColor="#ffffff"
android:gravity="center"
android:layout_marginRight="5dp"
android:background="#5ABC4F"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp" />
<Button
android:id="@+id/btn_search"
android:text="查 找"
android:textSize="25sp"
android:textColor="#ffffff"
android:gravity="center"
android:layout_marginLeft="5dp"
android:background="#5ABC4F"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp" />
</LinearLayout>
</RelativeLayout>
详细界面(增删改界面)布局:res/layout/student.xml
,在布局文件里面我们先将**查找**Button的Visibility属性设置为GONE,即不可见(注意区分:INVISIBLE为不可见但占据布局位置,GONE不可见且不占据位置)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@mipmap/boy"
android:id="@+id/student_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/student_name"
android:hint="输入姓名"
android:gravity="center"
android:textSize="20sp"
android:textColor="#5ABC4F"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/student_grade"
android:hint="输入分数"
android:gravity="center"
android:textSize="15sp"
android:textColor="#5ABC4F"
android:layout_marginTop="10dp"
android:layout_width=