android - 将变量传递给分页库类

标签 android retrofit android-paging

我正在使用 Android Paging Library 创建一个应用程序。我正在使用它进行改造。 改造代码在 ItemDataSource 中,我无法将变量传递给它。我有一些随 Intent 而来的变量。我如何在 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) {

                }
            });
               }
            }

主要 Activity

    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);

    }
 }

项目 View 模型

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/

最佳答案

为此,您必须在 ItemDataSource 类中创建一个构造函数,该类的一个新对象是在 ItemDataSourceFactory 中创建的,因此您必须在那里创建一个构造函数以获取值并将其传递给 ItemDataSource。并且您必须将值从 viewModel 传递给 ItemDataSourceFactory。这是它的外观(基于您发布的链接)

public class ItemViewModel extends ViewModel {
 LiveData<PagedList<Item>> itemPagedList;
LiveData<PageKeyedDataSource<Integer, Item>> liveDataSource;

public ItemViewModel(String table) {
    ItemDataSourceFactory itemDataSourceFactory = new ItemDataSourceFactory(table);
     liveDataSource = itemDataSourceFactory.getItemLiveDataSource();
     PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(ItemDataSource.PAGE_SIZE).build();
     itemPagedList = (new LivePagedListBuilder(itemDataSourceFactory,pagedListConfig))
            .build();
}}

然后在您的 Activity/fragment 中,您应该像这样传递值:

ItemViewModel itemViewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() {
        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T)new ItemViewModel ("your table name");
        }
    }).get(ItemViewModel.class);

关于android - 将变量传递给分页库类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52694980/

相关文章:

android - 如何上传文件到谷歌驱动器。是否可以通过 Retrofit 2

android - DataSource.Invalidate() 之后新的 PagedList 只有一页

android - DisplayListCanvas 在未绑定(bind)的 RenderNode 上启动(没有 mOwningView)

gson - 如何将 gson 转换为 LinkedHashMap<String, List<String>>?

android - 在请求运行时权限时选中“不再询问”框是否会禁用 future 的对话框?

android - 处理 Retrofit 2 RX 中的错误

android - 带分页库的无尽 RecyclerView 中的 AdMob 广告

Android Paging 3 不显示 Loadstate Adapter

java - 如何在 android/dalvik 上动态加载 Java 类?

android - 无法在 LinearLayout 内 WRAP_CONTENT RecyclerView