4

我创建了一个简单的Activity,它实现了LoaderManager.LoaderCallbacks<Cursor>接口。 的OnCreateLoader()很简单:当我打电话getLoaderManager().initLoader(0, null, this)创建Activity当光标变化时不会调用Android OnLoadFinished()

@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) 
{ 
    Log.d(TAG, "On create loader"); 

    Uri queryUri = ContentUris.withAppendedId(SmartPresentationMessage.Person.CONTENT_URI, 1); 
    CursorLoader cursorLoader = new CursorLoader(this, queryUri, null, null, null, null); 
    return cursorLoader; 
} 

该方法被调用。

我的问题是,在我的ContentProvider,它具有以下query()方法:

@Override 
public Cursor query(Uri uri, String[] projection, String where, 
     String[] whereArgs, String sortOrder) 
{ 
    int match = sUriMatcher.match(uri); 

    Cursor queryCursor; 
    SQLiteDatabase mdb = mOpenHelper.getReadableDatabase(); 

    switch (match) 
    { 
     case PERSON: 
      long personID = ContentUris.parseId(uri); 

      queryCursor = mdb.query(TABLE_NAME, projection, 
         SmartPresentationMessage.Person._ID + " = " + personID, 
         whereArgs, null, null, sortOrder); 

      asyncQueryRequest("" + (taskTag ++) , QUERY_URI + "/" + "person"); 

      return queryCursor; 
     default: 
      throw new IllegalArgumentException("unsupported uri: " + uri); 
    } 
} 

queryCursor获取数据库后,我从一个网络服务器的响应更新。 然而,当我打电话的ContentProvider的更新方法getContext().getContentResolver().notifyChange(uri, null)方法,对ActivityOnLoadFinished()方法不叫,即使URI是相同的实例化CursorLoader时使用的一个。

这是对ContentProvider更新方法:

@Override 
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) 
{ 
    //getContext().getContentResolver().notifyChange(uri, null); 


    // insert the initialValues into a new database row 
    SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 
    int affected; 
    try 
    { 
     switch (sUriMatcher.match(uri)) 
     { 
      case PERSON: 
       long personID = ContentUris.parseId(uri);; 
       affected = db.update(TABLE_NAME, values, 
         SmartPresentationMessage.Person._ID + " = " + personID, 
         whereArgs); 
       getContext().getContentResolver().notifyChange(uri, null); 

       break; 
      default: 
       throw new IllegalArgumentException("Unknown URI " + uri); 
     } 
    } 
    finally 
    { 
     db.close(); 
    } 

    return affected; 
} 

有人可以告诉我,这可能是我的OnLoadFinshed()方法的原因呼吁在相同的URI notifyChange()时不会被调用?

谢谢 克里斯提

回答

10

返回之前你Cursor在你的供应商的query()方法,需要调用下面的方法

cursor.setNotificationUri(contentResolver, uri); 

您可以通过Context得到ContentResolver。在ContentProvider这里有一个方法getContext()

相关问题