如何在 android 中使用 restartloader 和 listview
how to use restartloader in android with listview
我在列表视图中使用 Contnet 提供程序从 Sqlite 获取数据.. 当新记录插入列表视图时.. 记录在列表视图中显示而不刷新 activity... 这就是为什么我使用了 restartloader 但它不适用于我
数据库中的 getAllContacts() 我从数据库中获取总记录
public Cursor getAllPatients(){
return db.query(TABLE_NAME, new String[] { TAG_ID, TAG_NAME , TAG_AGE, TAG_CITY, TAG_MOB } ,
null, null, null, null,null);
}
这是内容提供商
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if (uriMatcher.match(uri)==PATIENTS){
myDb.getAllPatients().setNotificationUri(getContext().getContentResolver(), uri);
return myDb.getAllPatients();
}else {
return null;
}
}
这是 MainActivity
mAdapter = new SimpleCursorAdapter(getContext(),R.layout.status_item_layout,null,new String[]{
myDb.TAG_ID,myDb.TAG_NAME,myDb.TAG_AGE,myDb.TAG_CITY,myDb.TAG_MOB},
new int[]{R.id.tv_status_id,R.id.tv_status_name,R.id.tv_status_age,R.id.tv_status_city,R.id.tv_status_mob,},
0);
if(getLoaderManager().getLoader(0) == null){
getLoaderManager().initLoader(0, null, this).forceLoad();
}else{
getLoaderManager().restartLoader(0, null, this).forceLoad();
}
listView.setAdapter(mAdapter);
apiInterface = APIClient.getApiClient().create(ApiInterface.class);
return linearLayout;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = Patients.CONTENT_URI;
return new CursorLoader(getContext(),uri,null,null,null,null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
我不太清楚 restartLoader() 请告诉我哪里做错了?
就像@psking 评论的那样,restartLoader()
不需要更新游标数据列表中的更改,因为当涉及到带有 URI 的游标时,该过程被 Cursor's setNotificationUri() and ContentResolver's notifyChange() 方法取代。也就是说,我会做的是在你的 Activity 中坚持使用 initLoader()
来在运行时查询游标数据,然后在你的 ContentProvider 的 query()
方法中使用 setNotificationUri()
,并且 notifyChange()
在您的 ContentProvider 的 insert()
和 update()
方法中立即更新列表中的光标数据,如下所示:
public class MyProvider extends ContentProvider {
...
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
...
// Registers to watch a content URI for changes. This can be the URI of a specific
// data row (for example, "content://my_provider_type/23"), or a a generic URI for
// a content type.
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
...
// Notifies all listeners that the data has changed for the item content URI.
getContext().getContentResolver().notifyChange(uri, null);
// Returns the new URI with the ID (of the newly inserted row) appended at the end.
return ContentUris.withAppendedId(uri, id);
}
@Override
public int update(Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
...
// Notifies all listeners that the data at the given URI has changed should at least one
// row be updated.
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
}
我在列表视图中使用 Contnet 提供程序从 Sqlite 获取数据.. 当新记录插入列表视图时.. 记录在列表视图中显示而不刷新 activity... 这就是为什么我使用了 restartloader 但它不适用于我
数据库中的 getAllContacts() 我从数据库中获取总记录
public Cursor getAllPatients(){
return db.query(TABLE_NAME, new String[] { TAG_ID, TAG_NAME , TAG_AGE, TAG_CITY, TAG_MOB } ,
null, null, null, null,null);
}
这是内容提供商
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if (uriMatcher.match(uri)==PATIENTS){
myDb.getAllPatients().setNotificationUri(getContext().getContentResolver(), uri);
return myDb.getAllPatients();
}else {
return null;
}
}
这是 MainActivity
mAdapter = new SimpleCursorAdapter(getContext(),R.layout.status_item_layout,null,new String[]{
myDb.TAG_ID,myDb.TAG_NAME,myDb.TAG_AGE,myDb.TAG_CITY,myDb.TAG_MOB},
new int[]{R.id.tv_status_id,R.id.tv_status_name,R.id.tv_status_age,R.id.tv_status_city,R.id.tv_status_mob,},
0);
if(getLoaderManager().getLoader(0) == null){
getLoaderManager().initLoader(0, null, this).forceLoad();
}else{
getLoaderManager().restartLoader(0, null, this).forceLoad();
}
listView.setAdapter(mAdapter);
apiInterface = APIClient.getApiClient().create(ApiInterface.class);
return linearLayout;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = Patients.CONTENT_URI;
return new CursorLoader(getContext(),uri,null,null,null,null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
我不太清楚 restartLoader() 请告诉我哪里做错了?
就像@psking 评论的那样,restartLoader()
不需要更新游标数据列表中的更改,因为当涉及到带有 URI 的游标时,该过程被 Cursor's setNotificationUri() and ContentResolver's notifyChange() 方法取代。也就是说,我会做的是在你的 Activity 中坚持使用 initLoader()
来在运行时查询游标数据,然后在你的 ContentProvider 的 query()
方法中使用 setNotificationUri()
,并且 notifyChange()
在您的 ContentProvider 的 insert()
和 update()
方法中立即更新列表中的光标数据,如下所示:
public class MyProvider extends ContentProvider {
...
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
...
// Registers to watch a content URI for changes. This can be the URI of a specific
// data row (for example, "content://my_provider_type/23"), or a a generic URI for
// a content type.
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
...
// Notifies all listeners that the data has changed for the item content URI.
getContext().getContentResolver().notifyChange(uri, null);
// Returns the new URI with the ID (of the newly inserted row) appended at the end.
return ContentUris.withAppendedId(uri, id);
}
@Override
public int update(Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
...
// Notifies all listeners that the data at the given URI has changed should at least one
// row be updated.
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
}