如何在listview中使用android中的restartloader

时间:2017-07-06 15:02:25

标签: android sqlite android-contentprovider android-cursorloader

我在listview中使用Contnet提供程序从Sqlite获取数据..当listview中的新记录插入时,listview显示在listview中而没有刷新活动...这就是为什么我使用了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()的信息,请告诉我我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

就像@psking所评论的那样,更新游标数据列表中的更改不需要restartLoader(),因为当涉及到Cursor's setNotificationUri()ContentResolver's notifyChange()方法时,该过程被替换为relationship diagramenter image description here方法带有URI的游标。也就是说,我要做的是在Activity中坚持initLoader()以在运行时查询游标数据,然后在ContentProvider的setNotificationUri()方法中使用query()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;
    }
}