将变量传递给分页库类 [英] Passing variable to paging library class

查看:98
本文介绍了将变量传递给分页库类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Paging Library创建一个应用程序。我正在对其进行改造。
改造代码位于ItemDataSource中,我无法将变量传递给它。我有一些意向变量。如何在Retrofit Post方法中设置变量。

I am creating an app with Android Paging Library. I'm using retrofit with it. Retrofit code is in ItemDataSource and there i can't pass variable to it. I have some variable coming with intent. How can i set my variable in Retrofit Post method.

ItemDataSource

ItemDataSource

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 /

BTW i'm following this https://www.simplifiedcoding.net/android-paging-library-tutorial/

推荐答案

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

for doing this, you have to create a constructor in ItemDataSource class which you did, a new object of that class is created in ItemDataSourceFactory so you have to create a constructor there to get the value and pass it to ItemDataSource. and you have to pass the value to ItemDataSourceFactory from your viewModel. this is how it should look (based on the link that you posted)

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

然后在您的活动/片段中,应传递如下值:

then in your activity/fragment you should pass the value like this:

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

这篇关于将变量传递给分页库类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆