Search code examples
androidsqliteandroid-contentproviderandroid-cursorloader

how to use restartloader in android with listview


i get data from Sqlite using Contnet provider in listview .. when new record insert in listview.. record show in listview without refreshing activity... thats why i have used restartloader but it not work for me

getAllContacts() in Database i fetch total record from database

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

this is content provider

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

and this is 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);
   }

i have not clear know about restartLoader() please tell me where i am doing wrong?


Solution

  • Just like @psking commented, restartLoader() won't be necessary for updating changes in a list of cursor data since the process is replaced by Cursor's setNotificationUri() and ContentResolver's notifyChange() methods when it comes to cursors with URIs. That said, what I would do is stick with initLoader() in your Activity to query the cursor data at runtime, and then use setNotificationUri() in your ContentProvider's query() method, and notifyChange() in your ContentProvider's insert() and update() methods to update cursor data in your list instantly as follows:

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