Android:如何使用CursorAdapter?

时间:2010-08-23 05:19:23

标签: android android-cursoradapter

我有一个扩展ListView的数据库CustomCursorAdapterCursorAdapter。菜单按钮将项添加到数据库。我希望ListView更新并显示此更改。通常情况下,在我转到主屏幕并重新打开应用程序之前,它不会显示此新项目。

每当我添加新项目时,我最终通过调用cursor.requery()mCustomCursorAdapter.changeCursor(newCursor)来完成工作,但是当我在CursorAdapter构造函数中将autoRequery设置为false时,它只能工作相同。当autoRequery设置为false时,为什么会正确更新?

我正确使用CursorAdapter吗?使用数据库更新列表的标准方法是什么? autoRequery做了什么?

2 个答案:

答案 0 :(得分:37)

自动更新Cursor的惯用和非正确方法是在创建Cursor#setNotificationUri时以及在将它们传递给任何请求之前调用ContentResolver#notifyChange。当Cursor的Uri命名空间中的任何内容发生变化时,请调用AlarmProvider

例如,假设您正在创建一个简单的邮件应用程序,并且您希望在新邮件到达时更新,但也提供有关邮件的各种视图。我已经定义了一些基本的Uri。

content://org.example/all_mail
content://org.example/labels
content://org.example/messages

现在,我想要一个光标给我所有邮件,并在新邮件到达时更新:

Cursor c;
//code to get data
c.setNotificationUri(getContentResolver(), Uri.parse("content://org.example/all_mail");

现在新邮件到了,所以我通知:

//Do stuff to store in database
getContentResolver().notifyChange(Uri.parse("content://org.example/all_mail", null);

我还应该通知所有为此新消息符合的标签选择的Cursor

for(String label : message.getLabels() {
  getContentResolver().notifyChange(Uri.parse("content://org.example/lables/" + label, null);
}

而且,也许光标正在查看一条特定的消息,因此也要通知它们:

getContentResolver().notifyChange(Uri.parse("content://org.example/messages/" + message.getMessageId(), null);

getContentResolver()调用发生在访问数据的位置。因此,如果它位于您ServiceContentProvider的{​​{1}}或setNotificationUri中。您不应该从访问数据的位置执行此操作,例如notifyChange

{{3}}是一个简单的Activity,它使用此方法更新ContentProvider

答案 1 :(得分:1)

我为ListView更新创建了下一个方法:

/**
 * Method of refreshing Cursor, Adapter and ListView after database 
 * changing
 */
public void refreshListView() {
    databaseCursor = db.getReadableDatabase().query(
            CurrentTableName, 
            null, 
            null, 
            null, 
            null, 
            null, 
            "title"+SortingOrder);
    databaseListAdapter = new DomainAdapter(this, 
            android.R.layout.simple_list_item_2, 
            databaseCursor, 
            new String[] {"title", "description"}, 
            new int[] { android.R.id.text1, android.R.id.text2 });
    databaseListAdapter.notifyDataSetChanged();
    DomainView.setAdapter(databaseListAdapter);
}

每次在数据库中进行一些更改后都会调用它