I am using PagingDataAdapter to build my recyclerview but somehow in the mainactivity android studio shows an error that submitData is not a function of the respective adapter.
this is my code for my in the MainActivity:
viewModel = new ViewModelProvider(this ,
ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()))
.get(PhotoDataSourceFactory.class);
Lifecycle lifecycle = getLifecycle();
viewModel.setLifecycle(lifecycle);
recyclerView = findViewById(R.id.recyclerView);
PhotosAdapter adapter = new PhotosAdapter(Photos.CALLBACK);
recyclerView.setAdapter(adapter);
viewModel.getSearchResults().observe(this, listPagingData -> adapter.submitData(lifecycle, listPagingData));
My ViewModel :
public class PhotoDataSourceFactory extends ViewModel {
Lifecycle lifecycle;
private static final String TAG = "PhotoDataSourceFactory";
public PhotoDataSourceFactory() {
}
public void setLifecycle(Lifecycle lifecycle){
this.lifecycle = lifecycle;
}
public LiveData<PagingData<List<Photos>>> getSearchResults(){
PagingConfig config = new PagingConfig(20, 100, false);
Pager<Long , List<Photos>> pager = new Pager<>(config, new Function0<PagingSource<Long, List<Photos>>>() {
@Override
public PagingSource<Long, List<Photos>> invoke() {
return new PhotoSource();
}
});
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager),lifecycle);
}
}
My Adapter:
public class PhotosAdapter extends PagingDataAdapter<Photos , PhotosAdapter.PhotoViewHolder> {
private static final String TAG = "PhotosAdapter";
public PhotosAdapter(@NotNull DiffUtil.ItemCallback<Photos> diffCallback) {
super(diffCallback);
}
@NonNull
@Override
public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_photos,parent,false);
return new PhotoViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) {
Glide.with(holder.itemView.getContext()).load(getItem(position).getUrl()+".png").into(holder.photo);
holder.textView.setText(getItem(position).getTitle());
}
public class PhotoViewHolder extends RecyclerView.ViewHolder{
ImageView photo;
TextView textView;
public PhotoViewHolder(@NonNull View itemView) {
super(itemView);
photo = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
and My PagingSource:
public class PhotoSource extends ListenableFuturePagingSource<Long , List<Photos>> {
GetDataService dataService;
private static final String TAG = "PhotoSource";
@NotNull
@Override
public ListenableFuture<LoadResult<Long, List<Photos>>> loadFuture(@NotNull LoadParams<Long> loadParams) {
Executor executor = Executors.newSingleThreadExecutor();
dataService = RetrofitInstance.getInstance().create(GetDataService.class);
Long currentPage = loadParams.getKey()!=null ? loadParams.getKey() : (long)1;
ListenableFuture<List<Photos>> photos = dataService.getAllPhotos(currentPage);
ListenableFuture<LoadResult<Long , List<Photos>>> page = Futures.transform(photos, new Function<List<Photos>, LoadResult<Long, List<Photos>>>() {
@NullableDecl
@Override
public LoadResult.Page<Long, List<Photos>> apply(@NullableDecl List<Photos> input) {
return new LoadResult.Page<>(
Collections.singletonList(input),
currentPage == 1 ? currentPage : currentPage-1,
input.isEmpty() ? null : currentPage+1
);
}}, executor);
Log.d(TAG, "loadFuture: Heererereree");
ListenableFuture<LoadResult<Long , List<Photos>>> partialLoad = Futures.catching(page, HttpException.class, LoadResult.Error::new, executor);
return Futures.catching(partialLoad, IOException.class, LoadResult.Error::new, executor);
}
@Nullable
@Override
public Long getRefreshKey(@NotNull PagingState<Long, List<Photos>> pagingState) {
return null;
}
}
I don't understand where i am going wrong , tried to search alot on stackoverflow but all in vain , I wasn't able to glean anything of substance from google's documentation on paging 3 in java to resolve this issue. tried to find videos on paging 3 in java but unfortunately no one has covered paging in Java.
if there's anyone who has done this (paging 3) in java please if possible add a link to your github too thanks in advance :)