gpt4 book ai didi

android - 内容解析器 notifyChange() 不工作

转载 作者:太空狗 更新时间:2023-10-29 15:46:12 25 4
gpt4 key购买 nike

我设置了一个 fragment 以使用 CursorLoader 从自定义内容提供程序中提取数据。

问题是,当我使用内容解析器更新 SQLite 表中的记录时,光标不会刷新,即 getContext().getContentResolver().notifyChange(myUri, null) 有没有效果。我必须退出 fragment 并再次打开它才能看到变化。

我认为问题是我用来更新行的 URI 没有被加载程序观察到:

  • 创建加载器的 URI -content://com.myapp.provider/MyTable/Set/22
  • 更新行的 URI -content://com.myapp.provider/MyTable/167

167 标识表中的唯一行。 22 标识表中的一组行。 有没有什么方法可以告诉加载器第 167 行在集合 22 中,因此它应该重置游标?

这里是代码,以防它带来更多的清晰度:

在 fragment 中创建 CursorLoader:

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle queryBundle) {
CursorLoader cursorLoader = new CursorLoader(getActivity(), Uri.parse("content://com.myapp.provider/MyTable/Set/22"), myProjection, null, null, null);
return cursorLoader;
}

点击 fragment 中的按钮:

mContext.getContentResolver().update("content://com.myapp.provider/MyTable/167", values, null, null);

内容提供者类:

private static final String AUTHORITY = "com.myapp.provider";
private static final String TABLE_PATH = "MyTable";
public static final String CONTENT_URI_BASEPATH = "content://" + AUTHORITY + "/" + TABLE_PATH;

private static final int URITYPE_TABLE = 1;
private static final int URITYPE_SINGLE_SET = 2;
private static final int URITYPE_SINGLE_ROW = 3;

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, TABLE_PATH,URITYPE_TABLE);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#", URITYPE_SINGLE_SET);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/#", URITYPE_SINGLE_ROW);
}

@Override
public int update(Uri myUri, ContentValues values, String selection, String[] selectionArgs){
int rowCount = 0;
String id;
SQLiteDatabase db = localDB.getWritableDatabase();
int uriType = sUriMatcher.match(myUri);

switch(uriType){
case URITYPE_SINGLE_ROW :
id = uri.getLastPathSegment();
//selection and selectionArgs are ignored since the URI itself identifies a unique row.
rowCount = db.update(MyTable.TABLE_NAME, values, MyTable.COLUMN_ID + " = ?", new String[] {id});
}

getContext().getContentResolver().notifyChange(myUri, null);
return rowCount;
}

最佳答案

我有类似的问题并找到了 solution here .

简而言之,事实证明我需要对内容提供程序的 query() 方法返回的游标调用 setNotificationUri(ContentResolver cr, Uri uri)

关于android - 内容解析器 notifyChange() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24465999/

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