数据库数据列表显示
一、在Android开发中,使用的数据库一般是SQLite,至于有没有别的数据库,那我就不知道了,毕竟相对来说,我也只是一个初学者,对于SQLite的操作与其它数据库没有太大的区别,使用标准的SQL语句即可。
既然说到数据库,那么第一步肯定是建立数据库了,在开发Android的过程中,如果我们要建立数据库就要使用到SQLiteOpenHelper这个类,它是一个抽象类,使用它的过程中,我们要继承它,并使用它的子类进行库与表的创建。
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLOpenHelper extends SQLiteOpenHelper { public SQLOpenHelper(Context context) { /* * 父类的构造方法里面有四个参数 * 第一个参数是上下文环境:用于打开或者创建数据库 * 第二个是所要建立或修改的数据库名称 * 在Linux系统下,文件的后缀名只是个标识而已,没有实际的作用,也就是说你不加后缀名也可以照常使用该数据库 * 第三个是游标工厂,在这里,我们使用null,也就是直接使用默认的即可 * 第四个是版本号(从1开始):用于创建或升级数据库时使用,分别会对onCreate与onUpgrade方法进行操作 */ super(context, "hpu.db", null, 1); } public void onCreate(SQLiteDatabase db) { db.execSQL("create table person (id integer primary key autoincrement , name varchar(20) , phone varchar(11))"); } public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { db.execSQL("alter table person add amount intteger"); } }
那什么时候会调用上述的两个方法呢?当你获取数据库的时候,也就是当你调用getWritableDatabase或者是getReadableDatabase等方法的时候。
下面使用一个服务类,简单的描述一下,数据库的操作,多余的不解释,可以自己查阅API文档,了解各项参数的意思。
import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import cn.hpu.edu.pojo.Person; import cn.hpu.edu.util.SQLOpenHelper; public class PersonService { SQLOpenHelper helper = null; public PersonService(Context context) { helper = new SQLOpenHelper(context); } /** * 添加联系人 * @param person 需要添加的联系人信息 */ public void save(Person person) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("insert into person(name , phone) values (? , ?) ", new String[]{person.getName() , person.getPhone()}); db.close(); } /** * 删除联系人 * @param id 要删除联系人的Id */ public void delete(Integer id) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("delete from person where id = ?" , new String[]{String.valueOf(id)}); } /** * 更新联系人信息 * @param person 要更新的联系人的信息 */ public void update(Person person) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("update person set name = ? , phone = ? where id = ?", new String[]{person.getName() , person.getPhone() , String.valueOf(person.getId())}); } /** * 根据ID号查找联系人 * @param id 联系人的ID号 * @return 联系人的信息 */ public Person find(Integer id ) { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person where id = ?", new String[]{String.valueOf(id)}); cursor.moveToFirst(); Integer personid = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); Person person = new Person(personid , name , phone); return person; } /** * 获取部分联系人信息 * @param offset 跳过记录数量 * @param maxCount 最多查询记录数量 * @return 查询到的结果集 */ public List<Person> getScrollData(int offset , int maxCount) { List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person limit ? ,?", new String[]{String.valueOf(offset) ,String.valueOf(maxCount)}); while(cursor.moveToNext()) { Integer id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); Person person = new Person(id , name , phone); persons.add(person); } return persons; } }
二、数据的列表显示
第一步:在布局(layout)目录下面创建布局文件item.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/item"> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/personid" /> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/phone" /> </LinearLayout>
第二步:在activity_main.xml文件里面添加如下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:text="@string/id" /> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:text="@string/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/phone" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/listView"></ListView> </LinearLayout>
第三步:在MainActivity.java里面添加如下代码:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import cn.hpu.edu.pojo.Person; import cn.hpu.edu.service.PersonService; public class MainActivity extends Activity { private PersonService personService = null; private ListView listView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); personService = new PersonService(getApplicationContext()); listView = (ListView) findViewById(R.id.listView); show(); } private void show() { // 获取数据库中相应数据 List<Person> persons = personService.getScrollData(0, 20); // 处理所获取的数据 List<HashMap<String ,Object>> data = new ArrayList<HashMap<String ,Object>>(); for(Person person : persons) { HashMap<String , Object> item = new HashMap<String , Object>(); item.put("id", person.getId()); item.put("name", person.getName()); item.put("phone", person.getPhone()); data.add(item); } /* * 适配器的创建 * 第一个参数:上下文环境 * 第二个参数:标准格式的数据 * 第三个参数:对应布局文件 * 第四个参数:数据里面的各项属性 * 第五个参数:布局中组件的ID号,要与属性值相对应 */ SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"id", "name", "phone"}, new int[]{R.id.personid , R.id.name , R.id.phone}); listView.setAdapter(adapter); } }
完成之后,即可将应用布置到虚拟机上进行测试。