我正在努力实现以下目标。
1.通过游标加载器的活动将通过query()方法查询内容提供者。
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在其query()中将从网络获取一些数据。
3.我们将从响应中创建自定义矩阵光标,并使用初始数据集将光标返回到加载器。
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.当光标移动时(自定义光标已覆盖onMove并再次点击网络,而newPosition达到某个固定值以在用户滚动时获取其他数据)我们通过在其中添加一些行来更新光标。
5.通过notifyChange()API通知请求解析器以重新查询它。
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);
}
}
问题:
1.如果没有为大型实时数据建议最佳优化方法,这是正确的做事方式。
2.Calling the notify再次调用提供者的查询方法,结果返回初始数据集,而不是使用我在onMove中添加的初始数据集获取附加数据。
我想我已经把事情弄清楚了。请问用例是否有任何疑问