大佬教程收集整理的这篇文章主要介绍了删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
inMemoryElements
。当我删除一个项目时,我只是从列表中删除该项目,然后调用 Datasource.invalIDate(),它再次调用 LoadInitialCallBACk.onResult(),在其中传递我的缓存列表。@H_502_0@第一次删除一个项目时一切正常,但第二次删除另一个项目时,我得到:
@H_502_0@java.lang.indexoutofboundsexception: Index: 18,Size: 18 at java.util.ArrayList.get(ArrayList.java:437) at androIDx.paging.PagedStorage.get(PagedStorage.java:174) At androIDx.paging.PagedStorageDiffHelper$1.areItemsTheSame(PagedStorageDiffHelper.java:77)
@H_502_0@这些是我的组件:
getPagedList
来订阅 liveData<PagedList<ArticleDto>>
值。
invalIDateDatasource()
public class ArticleDatasource extends PageKeyedDatasource<Integer,ArticleDto> {
APIservice APIservice;
private List<ArticleDto> inMemoryElements;
public ArticleDatasource(APIservice APIservice,@R_675_10495@ng APIserviceMethod,List<ArticleDto> inMemoryElements,int currentPagE) {
this.APIservice = APIservice;
this.inMemoryElements = inMemoryElements;
this.currentPage = currentPage;
}
@OverrIDe
public voID loadInitial(@NonNull LoadInitialParams<Integer> params,@NonNull LoadInitialCallBACk<Integer,ArticleDto> callBACk) {
queryArticleDto.setPageIndex(currentPagE);
HashMap<@R_675_10495@ng,@R_675_10495@ng> headers = new HashMap<@R_675_10495@ng,@R_675_10495@ng>();
headers.put("Content-Type",httpConstants.http_CONTENT_TYPE);
if(inMemoryElements.isEmpty()){
APIservice.search(headers,queryArticleDto).enqueue(new CallBACk<PageResultDto<ArticleDto>>() {
@OverrIDe
public voID onResponse(Call<PageResultDto<ArticleDto>> call,Response<PageResultDto<ArticleDto>> responsE) {
inMemoryElements.addAll(response.body().getElements());
callBACk.onResult(response.body().getElements(),null,1);
}
@OverrIDe
public voID onFailure(Call<PageResultDto<ArticleDto>> call,Throwable t) {
callBACk.onResult(new ArrayList<>(),0);
}
});
}else{
callBACk.onResult(inMemoryElements,currentPage + 1);
}
}
@OverrIDe
public voID loadAfter(@NonNull LoadParams<Integer> params,@NonNull final LoadCallBACk<Integer,ArticleDto> callBACk) {
int currentPage = params.key;
queryArticleDto.setPageIndex(currentPagE);
APIservice.search(headers,queryArticleDto).enqueue(new CallBACk<PageResultDto<ArticleDto>>() {
@OverrIDe
public voID onResponse(Call<PageResultDto<ArticleDto>> call,Response<PageResultDto<ArticleDto>> responsE) {
if (response.issuccessful()) {
inMemoryElements.addAll(response.body().getElements());
callBACk.onResult(response.body().getElements(),currentPage + 1);
}
}
@OverrIDe
public voID onFailure(Call<PageResultDto<ArticleDto>> call,Throwable t) {
callBACk.onResult(new ArrayList<>(),currentPagE);
}
});
}
@H_502_0@}
@H_502_0@数据源工厂:
public class ArticleDatasourceFactory extends Datasource.Factory<Integer,ArticleDto> {
//To perform network calls
private APIservice APIservice;
private UserSessionliveData userSessionliveData;
private ArticleDto queryArticleDto;
private ArticleDatasource articleDatasource;
@Inject
public ArticleDatasourceFactory(APIservice APIservice,UserSessionliveData userSessionliveData) {
this.APIservice = APIservice;
thiS.UserSessionliveData = userSessionliveData;
}
public voID setqueryArticleDto(ArticleDto queryArticleDto) {
this.queryArticleDto = queryArticleDto;
if(articleDatasource != null)
articleDatasource.setqueryArticleDto(queryArticleDto);
}
public voID setAPIserviceMethod(@R_675_10495@ng APIserviceMethod) {
this.APIserviceMethod = APIserviceMethod;
}
//Factory method pattern implemented below
//Where a create method does the job of initializing the objects for clIEnt
@NonNull
@OverrIDe
public Datasource<Integer,ArticleDto> create() {
articleDatasource = new ArticleDatasource(APIservice,APIserviceMethod,userSessionliveData,articleDatasource == null ? new ArrayList<>() : articleDatasource.geTinR_889_11845@emoryElements(),articleDatasource == null ? 0 : articleDatasource.getCurrentPage()
);
articleDatasource.setqueryArticleDto(queryArticleDto);
return articleDatasource;
}
public ArticleDatasource getArticleDatasource() {
return articleDatasource;
}}
@H_502_0@分页列表:
public class ArticleRepository {
public liveData<PagedList<ArticleDto>> getPagedList(ArticleDto articleDto,@R_675_10495@ng APIserviceMethod) {
PagedList.Config config = new PagedList.Config.builder()
.setEnablePlaceholders(false)
.setPageSize(5)
.setPrefetchdistance(1)
.setMaxSize(10)
.build();
return new livePagedListBuilder<>(articleDatasourceFactory,config)
.setFetchExecutor(Executors.newFixedThreadPool(5)) // Use five threads to do the fetching operations
.build();
}}
@H_502_0@视图模型:
public class Articleviewmodel extends viewmodel {
private final mutablelivedata<PagedList<ArticleDto>> _pagedList = new mutablelivedata<>();
private liveData<PagedList<ArticleDto>> pagedList = (liveData<PagedList<ArticleDto>>) _pagedList;
public voID makeCallGetArticleListPaging(ArticleDto articleDto,@R_675_10495@ng APIserviceMethod){
pagedList = articleRepository.getPagedList(articleDto,APIserviceMethod);
}
public liveData<PagedList<ArticleDto>> getPagedList() {
return pagedList;
}}
@H_502_0@片段:
public class ArticleListFragment extends Fragment {
@OverrIDe
public voID onVIEwCreated(@NonNull View view,@Nullable Bundle savedInstanceStatE) {
articleviewmodel.makeCallGetArticleListPaging(articleDto,ArticleDatasource.API_serviCE_METHOD_SEARCH);
observeData();
}
private voID observeData() {
articleviewmodel.getPagedList().observe(getVIEwlifecycleowner(),new Observer<PagedList<ArticleDto>>() {
@OverrIDe
public voID onChanged(PagedList<ArticleDto> posts) {
postsPagedlistadapter.submitList(posts);
}
});
}
public voID invalIDateDatasource(int rowposition){
articleDatasourceFactory.getArticleDatasource().geTinR_889_11845@emoryElements().remove(rowposition);
articleDatasourceFactory.getArticleDatasource().invalIDate();
}}
@H_502_0@DiffUtil.ItemCallBACk
new DiffUtil.ItemCallBACk<ArticleDto>() {
@OverrIDe
public Boolean areItemsTheSame(@NonNull ArticleDto oldItem,@NonNull ArticleDto newItem) {
return oldItem.ID == newItem.ID;
}
@OverrIDe
public Boolean areContentsTheSame(@NonNull ArticleDto oldItem,@NonNull ArticleDto newItem) {
return oldItem.equals(newItem);
}
};
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误全部内容,希望文章能够帮你解决删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。