Android下数据库的操作应用(三)
来源:互联网 发布:印度火箭 知乎 编辑:程序博客网 时间:2024/05/16 09:53
> 本文接着上一篇:Android下数据库的操作应用(二),对数据库做一个综合应用的例子。以添加学生信息到数据库为例。
【5】数据库的综合应用。
- 在上一部分的代码基础上开始演示。先在activity_main.xml布局文件中添加一些控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入学生的姓名" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择学生的性别:" /> <RadioGroup android:id="@+id/rg_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_male" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/rb_female" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="女" /> </RadioGroup> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="save" android:text="保存" /> <LinearLayout android:id="@+id/ll_result" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > </LinearLayout></LinearLayout>
以上EditText用来添加学生,RadioGroup里的两个按钮用来选择添加学生的性别,当点击Button时,将学生信息保存到数据库中,同时将学生信息展示到下面的LinearLayout中。
- 接着在MainActivity中实现逻辑部分。
public class MainActivity extends Activity {private EditText et_name;private RadioGroup rg_sex;private StudentDao dao;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_name = (EditText) findViewById(R.id.et_name);rg_sex = (RadioGroup) findViewById(R.id.rg_sex);dao = new StudentDao(this);}public void save(View view) {String name = et_name.getText().toString().trim();if (TextUtils.isEmpty(name)) {Toast.makeText(this, "学生姓名不能为空", 0).show();return;}int id = rg_sex.getCheckedRadioButtonId();String sex = "male";if (id == R.id.rb_male) {// 男sex = "male";} else {// 女sex = "female";}dao.add(name, sex);Toast.makeText(this, "学生信息保存成功", 0).show();}}
要进行对编辑框中的学生信息做非空判断。判断学生的性别是男是女,再调用StudentDao里的add(name,sex)方法添加学生。
- 当我们往数据库里添加学生信息成功后,需要一个方法要取出所有学生信息。所以在StudentDao中添加一个方法findAll()方法,返回的数据类型是List类型。因为学生类信息不存在,所以需要重新创建一个Student类,我们将Student类放进新建的包com.beijing.studentinfo.domain中。
Student.java的代码如下:
package com.beijing.studentinfo.domain;/* * 学生信息的业务bean */public class Student {private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}
添加的findAll()方法的代码如下:
<span style="white-space:pre"></span>/* * 获取所有学生的信息 */public List<Student> findAll() {List<Student> students = new ArrayList<Student>();SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.rawQuery("select name,sex from student", null);while (cursor.moveToNext()) {String name = cursor.getString(0);String sex = cursor.getString(1);Student student = new Student();student.setName(name);student.setSex(sex);students.add(student);}cursor.close();db.close();return students;}
当我们在数据库中添加学生信息成功后,就可以通过findAll()方法将其用遍历的方式获取出来,然后展示到布局文件最下方的LinearLayout中。当然这里需要在Student类中重写toString()方法。
<span style="white-space:pre"></span>@Overridepublic String toString() {return "Student [name=" + name + ", sex=" + sex + "]";}
在上面的过程中需要在代码中创建一个TextView控件添加到LinearLayout中。
<span style="white-space:pre"></span>dao.add(name, sex);Toast.makeText(this, "学生信息保存成功", 0).show();<span style="white-space:pre"></span>List<Student> students = dao.findAll();for (Student student : students) {TextView textView = new TextView(this);textView.setText(student.toString());ll_result.addView(textView);}
此时,在EditText中添加学生信息,运行便能看到数据库中有新数据更新了。而且也会同步显示到activity_main.xml布局文件下方的LinearLayout中。不过有一个问题就是,每次运行展示的数据都会在原来的基础上叠加:
那该怎么解决这个问题呢?我们应该在执行了findAll()方法后,对数据进行remove操作,即对之前的数据做清除的工作。
<span style="white-space:pre"></span>List<Student> students = dao.findAll();ll_result.removeAllViews();//把之前的数据进行清除for (Student student : students) {
这样操作之后就不会出现上面叠加数据的情况了。但我们每次运行之后在LinearLayout中不会显示数据,即之前的数据没有同步保存显示出来。此时我们将显示数据的一部分代码抽取成一个方法,在onCreate()执行时就更新数据就可以了。抽取方法后为:
<span style="white-space:pre"></span>/* * 遍历数据库的全部数据,进行刷新显示 */private void refreshData() {List<Student> students = dao.findAll();ll_result.removeAllViews();//把之前的数据进行清除for (Student student : students) {TextView textView = new TextView(this);textView.setText(student.toString());ll_result.addView(textView);}}
在onCreate()方法进行更新:
<span style="white-space:pre"></span>@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_name = (EditText) findViewById(R.id.et_name);rg_sex = (RadioGroup) findViewById(R.id.rg_sex);ll_result = (LinearLayout) findViewById(R.id.ll_result);dao = new StudentDao(this);refreshData();//更新数据显示}
> 本文是数据库操作应用的部分内容,紧接上一部分:Android下数据库的操作应用(二)
> 本文的代码链接http://download.csdn.net/detail/programmerteny/9472312,可下载研究
0 0
- Android下数据库的操作应用(三)
- Android下数据库的操作应用(一)
- Android下数据库的操作应用(二)
- android assets文件夹下的数据库应用
- android下数据库操作
- Android数据库GreenDAO3.2.2的使用(三,GreenDao操作外部数据库----操作.db数据库)
- Android 开发应用SQLite数据库的简单使用(三)
- android 下创建操作数据库
- Android的NDK下原子操作函数的应用
- SQLite数据库的操作应用
- Android 应用的数据库
- android下数据库的创建和增删改查操作
- Android数据库的操作
- Android数据库的操作
- Android数据库的操作
- android 数据库的操作
- Android应用优化(7)数据库操作
- C#下的数据库操作
- 开发DataSnap服务器
- Codeforces 201E Thoroughly Bureaucratic Organization (智商题。。)
- c++第二次实验——学生成绩
- quartus modelsim-altera, Can't launch the Modelsim-Altera Software
- sbt.ResolveException: unresolved dependency: com.eed3si9n#sbt-assembly;0.9.2: not found
- Android下数据库的操作应用(三)
- 查临时表空间对应的数据文件
- QQ分享
- Android 中的 Service 全面总结
- iOS开发 - AFNetworking 的使用(第三方库)
- EAX、ECX、EDX、EBX寄存器的作用
- iOS9中新增App Transport Security(简称ATS)特性,
- java微信开发模板消息接口使用
- onMeasure()源码分析及自定义View对于wrap_content的支持