这是我的视图模型类中应该返回实时数据的方法.我的liveata总是返回null.
liveData<PagedList<APIResult>> getData(){ livePagedListProvIDer<Integer,APIResult> p = new livePagedListProvIDer<Integer,APIResult>() { @OverrIDe protected DataSource<Integer,APIResult> createDataSource() { return new DataClass(); } }; ListliveData = p.create(0,new PagedList.Config.Builder() .setPageSize(5) //number of items loaded at once .setPrefetchdistance(0)// the distance to the end of already loaded List before new data is loaded .build()); return ListliveData;}
这是Data类
public class DataClass extends TiledDataSource<APIResult> {private List<APIResult> result = new ArrayList<>();@OverrIDepublic int countItems() { return result.size();}@OverrIDepublic List<APIResult> loadRange(int startposition,int count) { Call<String> call = NetworkModule.provIDesWebService().makeRequest(); call.enqueue(new Callback<String>() { @OverrIDe public voID onResponse(@NonNull Call<String> call,@NonNull Response<String> response) { Log.i(DataClass.this.getClass().getSimplename() + " - onResponse",String.valueOf(response)); result = parseJson(response.body()); } @OverrIDe public voID onFailure(@NonNull Call<String> call,@NonNull Throwable t) { Log.i(DataClass.this.getClass().getSimplename() + " - onFailure",t.getMessage()); } }); return result;}
}
解决方法 我认为这有助于:1. countItems()应该返回DataSource.COUNT_UNdefineD
2. loadRange(int startposition,int count):您可以直接执行查询.
所以现在你可以删除结果全局变量
另外,关闭占位符:
ListliveData = p.create(0,new PagedList.Config.Builder() .setPageSize(5) //number of items loaded at once .setPrefetchdistance(10) //Must be >0 since placeholders are off .setEnablePlaceholders(false) .build());
以下是Data Class的更新代码:
public class DataClass extends TiledDataSource<APIResult> {@OverrIDepublic int countItems() { return DataSource.COUNT_UNdefineD;}@OverrIDepublic List<APIResult> loadRange(int startposition,int count) { Call<String> call = NetworkModule.provIDesWebService().makeRequest(); Response<String> response = call.execute(); return parseJson(response.body());}
您可以在此处查看示例项目:
https://github.com/brainail/.samples/tree/master/ArchPagingLibraryWithNetwork
以上是内存溢出为你收集整理的android – 没有房间的寻呼图书馆全部内容,希望文章能够帮你解决android – 没有房间的寻呼图书馆所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)