程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误?

开发过程中遇到删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误的问题如何解决?下面主要结合日常开发的经验,给出你关于删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误的解决方法建议,希望对你解决删除项目 PagedListAdapter,第二次删除列表项时出现 IndexOutOfBoundsException 错误有所启发或帮助; @H_502_0@我正在删除 Pagedlistadapter 中的一个项目。我没有使用房间,我只是存储我加载的所有元素的缓存列表 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@这些是我的组件:

@H_607_10@
  • @H_502_0@在我的 Fragment 中,我有分页的 RecyclerVIEw,在这里我调用 viewmodel 方法 @H_260_2@makeCallGetArticleListPaging 将元素加载到 liveData 包装中。然后我调用函数 getPagedList 来订阅 liveData<PagedList<ArticleDto>> 值。

  • @H_502_0@在 DatasourceFactory 中:我有一个当前数据源的参,用于调用 .invalIDate()

  • @H_502_0@Reposiroty:我在其中创建 pagedList。

  • @H_502_0@最后当 item 被删除时,我调用 viewmodel 方法 invalIDateDatasource()

  • @H_502_0@数据源:

    public class ArticleDatasource extends PageKeyedDatasource<Integer,ArticleDto> {
    
     APIservice APIservice;
     private List<ArticleDto> inMemoryElements;
    
     public ArticleDatasource(APIservice APIservice,String 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<String,String> headers = new HashMap<String,String>();
        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(String 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_558_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,String 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,String 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_558_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,请注明来意。