How to send search suggestions dynamically to data source and get updated pagelist in Android jetpack paging library?

狂风中的少年 提交于 2020-01-06 07:56:08

问题


How to send search suggestions dynamically to data source's retrofit params and get updated pagedlist in Android jetpack paging library?

Here's my web function in retrofit web service that brings data.

@GET(version + "/get-bills")
  Call<ApiResponse<BillsModel>> getPartnerBillsSorted(@Query("page")int page, @Query("type")int  type,@Query("search")String search );

This api is being called in data source and it gives me listing of all data, which i am listing in reyclerview using pagedlist. But i am quite confused and dont know how do i give call to this api in Data source on runtime, and then get updated pagedlist based on what i am seaching in autocompleteTextview.

what should i do? should i make new instance of datasource and data source factory and pagelist everytime i tap something in autocomplete or how do i dynamically change same datasource call and get updated pagelist?


回答1:


You do not need to to change anything in your web service. You may make a constant class(getter setter) etc and change your data source class like this.

compositeDisposable.add(
            networkService.getPartnerBillsSorted(Costant.PAGE,
            ,Costant.TYPE
            ,Costant.SEARCH
            .subscribe()

and then call this in your search complete listener in activity/fragment:

 Constants.setSearch("search result");
 viewModel.searchResultList.value.dataSource.invalidate();



回答2:


create a field in your Datasource class(add getter, setters), pass this field to retrofit call. later if you want make another call just set the required search query to datasource instance and call .invalidate() after setting the query.

refer this article https://android.jlelse.eu/android-paging-library-make-your-lists-as-efficient-as-possible-literally-in-just-an-hour-5abf797585bd



来源:https://stackoverflow.com/questions/54457490/how-to-send-search-suggestions-dynamically-to-data-source-and-get-updated-pageli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!