Android Paging Library を使用してアプリを作成しています。私はそれでレトロフィットを使用しています。改造コードは ItemDataSource にあり、そこに変数を渡すことができません。私は意図的にいくつかの変数を持っています。Retrofit Post メソッドで変数を設定するにはどうすればよいですか。
アイテムデータソース
public class ItemDataSource extends PageKeyedDataSource<Integer, Item> {
//we will start from the first page which is 1
private static final int PAGE_NUMBER = 1;
//this will be called once to load the initial data
String table
ItemDataSource(String table){
this.table = table;
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull
final LoadInitialCallback<Integer, Item> callback) {
RetrofitClient.getInstance()
// I want to pass table variable here.
.getApi().getAnswers("table","","","",PAGE_NUMBER,"")
.enqueue(new Callback<StackApiResponse>() {
@Override
public void onResponse(Call<StackApiResponse> call,
Response<StackApiResponse> response) {
if (response.body() != null) {
callback.onResult(response.body().images, null,
PAGE_NUMBER + 1);
}
}
@Override
public void onFailure(Call<StackApiResponse> call,
Throwable
t) {
}
});
}
}
主な活動
public class Detail extends AppCompatActivity {
ArrayList<Item> items;
Api api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// I'm getting intent here.
final RecyclerView recyclerView =
findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
ItemViewModel itemViewModel =
ViewModelProviders.of(this).get(ItemViewModel.class);
//creating the Adapter
//observing the itemPagedList from view model
itemViewModel.itemPagedList.observe(this, new
Observer<PagedList<Item>>() {
@Override
public void onChanged(@Nullable PagedList<Item> items) {
//in case of any changes
//submitting the items to adapter
adapter.submitList(items);
}
});
//setting the adapter
recyclerView.setAdapter(adapter);
}
}
アイテム ビュー モデル
public class ItemViewModel extends ViewModel {
//creating livedata for PagedList and PagedKeyedDataSource
LiveData<PagedList<Item>> itemPagedList;
LiveData<PageKeyedDataSource<Integer, Item>> liveDataSource;
//constructor
public ItemViewModel() {
//getting our data source factory
ItemDataSourceFactory itemDataSourceFactory = new
ItemDataSourceFactory();
//getting the live data source from data source factory
liveDataSource = itemDataSourceFactory.getItemLiveDataSource();
//Getting PagedList config
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(10).build();
//Building the paged list
itemPagedList = (new LivePagedListBuilder(itemDataSourceFactory,
pagedListConfig))
.build();
}
}
ところで、私はこれに従っています https://www.simplifiedcoding.net/android-paging-library-tutorial/