为什么cursorLoader没有通知更改源数据? [英] Why cursorLoader didn't notify changes in source data?
问题描述
我有一个简单的ContentProvider,有一个ListView的布局和添加内容提供商与CursorLoader项目的按钮。该<一href="http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished%28android.content.Loader" rel="nofollow">http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader, D)引用指出,
I have a simple contentProvider, a layout with a ListView and a button for adding Items in content Provider and a CursorLoader. The http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader, D) reference states that
装载程序将监视更改数据,并报告给 你经过这里新的呼叫。你不应该监视自己的数据。 例如,如果数据是一个游标,你将其放置在一个 CursorAdapter的,使用的CursorAdapter(android.content.Context, android.database.Cursor,int)构造不传递在任 FLAG_AUTO_REQUERY或FLAG_REGISTER_CONTENT_OBSERVER(即,使用0 为flags参数)。这prevents从做的CursorAdapter 自己的观察光标,自当不需要 变化发生了,你会得到一个新的光标抛出另一个来电来访。
The Loader will monitor for changes to the data, and report them to you through new calls here. You should not monitor the data yourself. For example, if the data is a Cursor and you place it in a CursorAdapter, use the CursorAdapter(android.content.Context, android.database.Cursor, int) constructor without passing in either FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0 for the flags argument). This prevents the CursorAdapter from doing its own observing of the Cursor, which is not needed since when a change happens you will get a new Cursor throw another call here.
但Log.info线在onLoadFinished方法未执行和ListView没有刷新。这里是我的(简单)code:
But the Log.info line in the onLoadFinished method was not executed and listView didn't refreshed. Here is my (simple) code:
public class HomeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{
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<Cursor> 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<Cursor> 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<Cursor> 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);
}
}
任何建议?
推荐答案
AFAIK,你需要实现的ContentProvider
的通知自己。对于这一点,添加类似的getContext()getContentResolver()有NotifyChange(URI,空);
到插入
, 更新
和删除
的ContentProvider
的方法,并调用 .setNotificationUri(的getContext()。getContentResolver(),URI)
在光标
在查询
在<一个描述href="http://developer.android.com/reference/android/content/ContentProvider.html#query%28android.net.Uri,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String%29"相对=nofollow>官方文档。 这里你可以找到整个图片。
AFAIK, you need to implement notification yourself in ContentProvider
. For this, add something like getContext().getContentResolver().notifyChange(uri, null);
to insert
,update
and delete
method of ContentProvider
and invoke.setNotificationUri(getContext().getContentResolver(), uri)
on your Cursor
in query
as described in official documentation. Here you can find the whole picture.
注意::您不应该关闭游标( cursor.close()
),以获得
有关变更的通知。
Note: You should not close the cursor (cursor.close()
) in order to get
notifications about the changes.
这篇关于为什么cursorLoader没有通知更改源数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!