gpt4 book ai didi

android - 为什么 cursorLoader 没有通知源数据的变化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:28 25 4
gpt4 key购买 nike

我有一个简单的 contentProvider,一个带有 ListView 的布局和一个用于在内容 Provider 和 CursorLoader 中添加项目的按钮。 android.content.Loader, D引用文献指出

The Loader will monitor for changes to the data, and report them toyou through new calls here. You should not monitor the data yourself.For example, if the data is a Cursor and you place it in aCursorAdapter, use the CursorAdapter(android.content.Context,android.database.Cursor, int) constructor without passing in eitherFLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0for the flags argument). This prevents the CursorAdapter from doingits own observing of the Cursor, which is not needed since when achange happens you will get a new Cursor throw another call here.

但是onLoadFinished 方法中的Log.info 行没有执行,listView 也没有刷新。这是我的(简单的)代码:

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);
}
}

有什么建议吗?

最佳答案

据我所知,您需要自己在 ContentProvider 中实现通知。为此,将 getContext().getContentResolver().notifyChange(uri, null); 添加到 insertupdate删除 ContentProvider 方法并在 中的 Cursor 上调用 .setNotificationUri(getContext().getContentResolver(), uri) >查询official documentation中所述. Here你可以找到全貌。

注意:您不应该关闭游标(cursor.close())以获得 有关更改的通知。

关于android - 为什么 cursorLoader 没有通知源数据的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com