Почему cursorLoader не уведомляет об изменениях в исходных данных?

У меня есть простой contentProvider, макет с ListView и кнопкой для добавления элементов в содержимое Provider и CursorLoader. Ссылка http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader, D) утверждает, что

Загрузчик будет следить за изменениями данных и сообщать о них вам через новые вызовы здесь. Вы не должны сами отслеживать данные. Например, если данные представляют собой курсор и вы помещаете их в CursorAdapter, используйте CursorAdapter(android.content.Context, android.database.Cursor, int) конструктор, не передавая ни FLAG_AUTO_REQUERY или FLAG_REGISTER_CONTENT_OBSERVER (то есть, используйте 0 для аргумента flags). Это не позволит CursorAdapter выполнять собственное наблюдение за курсором, что не нужно, так как при произойдет изменение, вы получите новый Cursor, бросив еще один вызов здесь.

Но строка Log.info в методе onLoadFinished не была выполнена и listView не обновился. Вот мой (простой) код:

public class HomeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks{
static final String TAG = "HomeActivity";
SimpleCursorAdapter adapter;
ListView listAnnunciVicini;

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

    listAnnunciVicini = (ListView) findViewById(R.id.lista_annunci_vicini);

    adapter = new SimpleCursorAdapter(this, R.layout.list_item, null, 
            new String[] {
                    ContentDescriptor.Annunci.Cols.ID, 
                    ContentDescriptor.Annunci.Cols.TITOLO, 
                    ContentDescriptor.Annunci.Cols.DESCRIZIONE 
            }, new int[] { 
                    R.id.list_annunci_item_id_annuncio, 
                    R.id.list_annunci_item_titolo_annuncio,
                    R.id.list_annunci_item_descrizione_annuncio
            }, 0);
    listAnnunciVicini.setAdapter(adapter);

    // Prepare the loader. Either re-connect with an existing one,
    // or start a new one.
    getSupportLoaderManager().initLoader(0, null, this).forceLoad();
}

public void addRandomItem(View sender) {
    ContentValues dataToAdd = new ContentValues();
    dataToAdd.put(ContentDescriptor.Annunci.Cols.TITOLO, "Titolo");
    dataToAdd.put(ContentDescriptor.Annunci.Cols.DESCRIZIONE, "Lorem ipsum dolor sit amet.");
    this.getContentResolver().insert(ContentDescriptor.Annunci.CONTENT_URI, dataToAdd);
}

@Override
public Loader onCreateLoader(int id, Bundle args) {
    // creating a Cursor for the data being displayed.
    String[] proiezione = new String[] {ContentDescriptor.Annunci.Cols.ID, ContentDescriptor.Annunci.Cols.TITOLO, ContentDescriptor.Annunci.Cols.DESCRIZIONE };

    CursorLoader cl = new CursorLoader(this, ContentDescriptor.Annunci.CONTENT_URI, proiezione, null, null, null);
    return cl;
}

public void onLoadFinished(Loader loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    adapter.swapCursor(data);
    Log.i(TAG, "I dati sono stati ricaricati");
}

public void onLoaderReset(Loader loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    adapter.swapCursor(null);
}
}

Есть предложения?

7
задан tshepang 20 May 2014 в 19:12
поделиться