1

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

  • ad 2) you could reuse your `MyCursor` objects (based on the input `Uri`) so that you dont have to create a new one each time `query` is called, but you can also use a custom `AbstractWindowedCursor` – pskink Dec 22 '16 at 12:22
  • Can't reuse MyCursor objects because the same functionality is used for another fragment of ViewPager with different data set (Reusing the MyCursor will return the same data for all ViewPager fragments) – Pawan Shukla Dec 23 '16 at 05:51
  • no, if you use the different `Uri`s, or as i said use a custom `AbstractWindowedCursor` – pskink Dec 23 '16 at 05:52
  • Also not sure what level of extensive work is required to create a custom AbstractWindowedCursor. – Pawan Shukla Dec 23 '16 at 05:53
  • see `SqliteCursor` sources on how they did it with `onMove` method: http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/database/sqlite/SQLiteCursor.java#119 – pskink Dec 23 '16 at 05:56

0 Answers0