微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个简单的contentProvider,一个带有ListView的布局和一个用于在内容Provider和CursorLoader中添加Items的按钮. http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader,D)参考说明了这一点

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.

但是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,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.DESCRIZIONE };

    CursorLoader cl = new CursorLoader(this,ContentDescriptor.Annunci.CONTENT_URI,proiezione,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,null);插入,更新和删除ContentProvider的方法和invoke.setNotificationUri(getContext().getContentResolver(),uri)在 official documentation. Here中描述的查询中你可以找到整个图片.

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

原文地址:https://www.jb51.cc/android/316677.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐