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
原创粉丝点击