Android中SQLite应用详解(7)

2019-01-26 21:52

private DBManager mgr; private ListView listView;

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

listView = (ListView) findViewById(R.id.listView); //初始化DBManager

mgr = new DBManager(this); }

@Override

protected void onDestroy() { }

public void add(View view) {

ArrayList persons = new ArrayList();

Person person1 = new Person(\Person person2 = new Person(\Person person3 = new Person(\Person person4 = new Person(\Person person5 = new Person(\

persons.add(person1); persons.add(person2); persons.add(person3); persons.add(person4); persons.add(person5);

mgr.add(persons); super.onDestroy();

//应用的最后一个Activity关闭时应释放DB mgr.closeDB();

}

public void update(View view) { }

public void delete(View view) { }

public void query(View view) { }

public void queryTheCursor(View view) {

List persons = mgr.query();

ArrayList> list = new ArrayList

SimpleAdapter adapter = new SimpleAdapter(this, list,

new String[]{\

HashMap map = new HashMap

map.put(\list.add(map);

Person person = new Person(); person.age = 30;

mgr.deleteOldPerson(person); Person person = new Person(); person.name = \person.age = 30; mgr.updateAge(person);

String>>();

String>();

person.info);

android.R.layout.simple_list_item_2,

int[]{android.R.id.text1, android.R.id.text2});

listView.setAdapter(adapter);

{ } }

Cursor c = mgr.queryTheCursor(); startManagingCursor(c);

//托付给activity根据自己的生命

周期去管理Cursor的生命周期

CursorWrapper cursorWrapper = new CursorWrapper(c) { };

//确保查询结果中有\列

SimpleCursorAdapter adapter = new

cursorWrapper, new String[]{\

@Override

public String getString(int columnIndex) { }

//将简介前加上年龄

if (getColumnName(columnIndex).equals(\ }

return super.getString(columnIndex);

int age = getInt(getColumnIndex(\return age + \

super.getString(columnIndex);

SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, \

ListView listView = (ListView) listView.setAdapter(adapter);

findViewById(R.id.listView);

这里需要注意的是SimpleCursorAdapter的应用,当我们使用这个适配器时,我们必须先得到一个Cursor对象,这里面有几个问题:如何管理Cursor的生命周期,如果包装Cursor,Cursor结果集都需要注意什么。

如果手动去管理Cursor的话会非常的麻烦,还有一定的风险,处理不当的话运行期间就会出现异常,幸好Activity为我们提供了startManagingCursor(Cursor cursor)方法,它会根据Activity的生命周期去管理当前的Cursor对象,下面是该方法的说明:

[java]view plaincopyprint?

1. /**

2. * This method allows the activity to take care of managing the gi

ven

3. * {@link Cursor}'s lifecycle for you based on the activity's life

cycle.

4. * That is, when the activity is stopped it will automatically cal

l

5. * {@link Cursor#deactivate} on the given Cursor, and when it is l

ater restarted

6. * it will call {@link Cursor#requery} for you. When the activity

is

7. * destroyed, all managed Cursors will be closed automatically. 8. *

9. * @param c The Cursor to be managed. 10. *

11. * @see #managedQuery(android.net.Uri , String[], String, String[]

, String)

12. * @see #stopManagingCursor 13. */

/**

* This method allows the activity to take care of managing the given * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.

* That is, when the activity is stopped it will automatically call * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted

* it will call {@link Cursor#requery} for you. When the activity is

* destroyed, all managed Cursors will be closed automatically. *

* @param c The Cursor to be managed. *

* @see #managedQuery(android.net.Uri , String[], String, String[], String)

* @see #stopManagingCursor */

文中提到,startManagingCursor方法会根据Activity的生命周期去管理当前的Cursor对象的生命周期,就是说当Activity停止时他会自动调用Cursor的deactivate方法,禁用游标,当Activity重新回到屏幕时它会调用Cursor的requery方法再次查询,当Activity摧毁时,被管理的Cursor都会自动关闭释放。

如何包装Cursor:我们会使用到CursorWrapper对象去包装我们的Cursor对象,实现我们需要的数据转换工作,这个CursorWrapper实际上是实现了Cursor接口。我们查询获取到的Cursor其实是Cursor的引用,而系统实际返回给我们的必然是Cursor接口的一个实现类的对象实例,我们用CursorWrapper包装这个实例,然后再使用SimpleCursorAdapter将结果显示到列表上。

Cursor结果集需要注意些什么:一个最需要注意的是,在我们的结果集中必须要包含一个“_id”的列,否则SimpleCursorAdapter就会翻脸不认人,为什么一定要这样呢?因为这源于SQLite的规范,主键以“_id”为标准。解决办法有三:第一,建表时根据规范去做;第二,查询时用别名,例如:SELECT id AS _id FROM person;第三,在CursorWrapper里做文章:

[java]view plaincopyprint?

1. CursorWrapper cursorWrapper = new CursorWrapper(c) { 2. @Override

3. public int getColumnIndexOrThrow(String columnName) throws Illegal

ArgumentException {

4. if (columnName.equals(\5. return super.getColumnIndex(\6. }

7. return super.getColumnIndexOrThrow(columnName); 8. } 9. };

CursorWrapper cursorWrapper = new CursorWrapper(c) {

@Override

public int getColumnIndexOrThrow(String columnName)

if (columnName.equals(\ } return

return super.getColumnIndex(\

throws IllegalArgumentException {

super.getColumnIndexOrThrow(columnName);

};

}

如果试图从CursorWrapper里获取“_id”对应的列索引,我们就返回查询结果里“id”对应的列索引即可。

最后我们来看一下结果如何:


Android中SQLite应用详解(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新环保法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: