简体   繁体   English

如何使用 Android 分页库将意图变量传递给 retrofit

[英]How to pass an intent variable to retrofit using Android Pagination Library

I am implementing android pagination library in my app and would like to pass "id" of an item from my activity to the data source where my network call is made我正在我的应用程序中实现 android 分页库,并希望将项目的“id”从我的活动传递到进行网络调用的数据源

AddCommentActivity.java AddCommentActivity.java

//I want to pass this string to the network call.
String image_id = getIntent().getStringExtra("image_id");

CommentViewModel commentViewModel = new ViewModelProvider(this).get(CommentViewModel.class);

CommentDataSource.java CommentDataSource.java

public class CommentDataSource extends PageKeyedDataSource<Long, Comment> {
    public CommentDataSource(){
    progress_bar = new MutableLiveData<>();
     } 

   @Override
public void loadInitial(@NonNull final LoadInitialParams<Long> params, @NonNull final LoadInitialCallback<Long, Comment> callback) {
    RestApi restApi = RetrofitApi.create();

    Call<CommentResponse> call = restApi.getComments(FIRST_PAGE, "I want the image_id from activity here");

    call.enqueue(new Callback<CommentResponse>() {
        @Override
        public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {

 }

CommentDataSourceFactory.java评论DataSourceFactory.java

public class CommentDataFactory extends DataSource.Factory<Long, Comment> {

public MutableLiveData<CommentDataSource> commentLiveDataSource = new MutableLiveData<>();

public CommentDataFactory() {
}

@Override
public DataSource<Long, Comment> create() {
    CommentDataSource commentDataSource = new CommentDataSource();
    commentLiveDataSource.postValue(commentDataSource);
    return commentDataSource;
}

CommentViewModel.java评论查看型号.java

public class CommentViewModel extends ViewModel {

public  LiveData<PagedList<Comment>> commentPagedList;
public  LiveData<CommentDataSource> liveDataSource;
public  LiveData progressBar;

public CommentViewModel(){
    CommentDataFactory commentDataFactory = new CommentDataFactory();
    liveDataSource = commentDataFactory.commentLiveDataSource;

    progressBar = Transformations.switchMap(liveDataSource, CommentDataSource::getProgressBar);

    PagedList.Config config = new PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setPageSize(CommentDataSource.PAGE_SIZE)
            .build();

    commentPagedList = new LivePagedListBuilder<>(commentDataFactory, config).build();
}


public LiveData<PagedList<Comment>> getCommentData(){
    return commentPagedList;
}

public void getRefreshedData(){
    getCommentData().getValue().getDataSource().invalidate();
}
}

How to do that.?怎么做。? I checked Passing variable to paging library class which is exactly what I want to do but I dont understand it and the code gives errors.我检查了将变量传递给分页库 class这正是我想要做的,但我不明白它并且代码给出了错误。 Errors such as错误如

Cannot create an instance of class CommentViewModel无法创建 class CommentViewModel 的实例

CommentViewModel has no zero argument constructor CommentViewModel 没有零参数构造函数

Okay do:好吧:

 commentViewmodel1.getCommentData().observe(this, new Observer<PagedList<Comments>>(){
 @Override
public void onChanged(PagedList<Comment> 
    comments){
      adapter.submitList(comments);

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM