Android: how to use CursorAdapter?

后端 未结 2 696
鱼传尺愫
鱼传尺愫 2020-12-07 16:03

I have a database, a ListView, and a CustomCursorAdapter that extends CursorAdapter. A menu button adds an item to the database. I wan

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 16:46

    The idiomatic and imho correct way to automatically update Cursors is to call Cursor#setNotificationUri when they are created and before they are handed off to whatever requested them. Then call ContentResolver#notifyChange when anything in that Cursor's Uri's namespace changes.

    For example, suppose you were creating a simple mail application and you wanted to update when new mail arrived but also provide various views on the mail. I'd have some basic Uri's defined.

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

    Now, say I wanted to get a cursor that gave me all mail and be updated when new mail arrives:

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

    Now new mail arrives so I notify:

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

    I should also notify all the Cursors that selected for labels this new message met

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

    And also, maybe a cursor is viewing that one specific message so notify them as well:

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

    The getContentResolver() calls happen where the data is accessed. So if it's in a Service or ContentProvider that is where you setNotificationUri and notifyChange. You should not be doing that from where the data is accessed, e.g., an Activity.

    AlarmProvider is a simple ContentProvider that uses this method to update Cursors.

提交回复
热议问题