Creating Network Based Content Provider

269 views Asked by At

I am trying to achieve the following things.

1.Activity via cursor loader will query to the content provider by query() method.

public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    CursorLoader cursorLoader = new CursorLoader(getActivity(), MyProvider.toUri(MyApis.FILTER), new String[]{baseUrl, categoryCode}, MyApis.XYZ, null, null);
    return cursorLoader;
}

2.Content Provider in its query() will get some data from network.

3.We will create the Custom Matrix Cursor from response and return the cursor to the loader with initial set of data.

  public Cursor query(Uri uri, String[] serviceParams, String serviceApiName, String[] selectionArgs, String sortOrder) {     

    RestAdapter retrofit = new RestAdapter.Builder().setEndpoint(serviceParams[0]).setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new SimpleXMLConverter()).build();
    MyResponse response = retrofit.create(MyApis.class).filter(getFilterParams(serviceParams[1]));
    MyMatrixCursor cursor = new MyMatrixCursor (new String[]{"_id", "cat", "name", "imageURI", "free", "year", "runtime", "stars"}, getContext(), uri, serviceApiName, serviceParams);

    List<Thumbnails> thumbnailsList = response.getThumbnails();
    for (Thumbnails thumbnails : thumbnailsList) {
        cursor.addRow(new Object[]{thumbnails.getId(), thumbnails.getCat(), thumbnails.getName(), thumbnails.getImageURI(), thumbnails.getFree(), thumbnails.getYear(), thumbnails.getRuntime(), thumbnails.getStars()});
    }
   return cursor;
}

4.While movement of the cursor (Custom Cursor which has override the onMove and hit the network again while newPosition reaches to a certain fixed value to get additional data while user scrolls)we update the cursor by adding some rows into it.

5.Notify the resolver by notifyChange() API to requery it.

public class MyCursor extends MatrixCursor {

public MyCursor (String[] columnNames, Context mContext, Uri uri,
                 String serviceApiName, String[] serviceParams) {
    super(columnNames);
    this.mContext = mContext;
    this.uri = uri;
    this.serviceApiName = serviceApiName;
    this.serviceParams = serviceParams;
    setNotificationUri(mContext.getContentResolver(), uri);      
}

@Override
public boolean onMove(int oldPosition, int newPosition) {    

    Log.d(TAG, "Old Position : " + oldPosition + " New Position : " + newPosition + " Count " + getCount());

    if(newPosition == getCount()-1){

        //Suppose this data comes from network asynchronously
        addRow(new Object[]{1010, "Category", "Name", "ImageUrl", true,"2012","Android","5"});     

        mContext.getContentResolver().notifyChange(uri, null);
    }      
    return super.onMove(oldPosition, newPosition);
}   

}

Problems :

1.Is this the right way of doing the things if not suggest the best optimized approach for large set real time data.

2.Calling the notify calls the query method of provider again which results to return with the initial set of data instead of getting the additional data with initial set of data which I added in onMove.

I think i have made the things clear.Please ask if any doubt in use case

0

There are 0 answers