您好,我是MVVM
架构的新手,我想弄清楚为什么我的列表大小显示为零,但数据却显示在recyclerview
中,我正在使用分页库进行分页,以及如何显示如果后端的列表为空,则会出现错误响应,如何在将MVVM
设计模式与分页库一起使用时显示其他响应
public class Myjobsviewmodel extends ViewModel {
public LiveData myjobslivedata;
public LiveData<PageKeyedDataSource<Integer, MyJobspojo.Job>> myjobsdatasource;
public MyJobsFactory itemDataSourceFactory;
public Myjobsviewmodel() {
itemDataSourceFactory = new MyJobsFactory();
myjobsdatasource = itemDataSourceFactory.getItemLiveDataSource();
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPageSize(MyJobsDataSource.per_page)
.build();
myjobslivedata = (new LivePagedListBuilder(itemDataSourceFactory, pagedListConfig))
.build();
}
// added for swipe refresh layout
public void refreshmyjobs(){
itemDataSourceFactory.getItemLiveDataSource().getValue().invalidate();
}
public LiveData<PagedList<MyJobspojo.Job>> getMoviesInTheaterList() {
return myjobslivedata;
}
public class MyJobs extends Fragment {
FloatingActionButton button;
RecyclerView myjobsview;
MyJobsAdapter myjobsadapter;
ArrayList<MyJobspojo.Job> myjobslist;
public static int navItemIndex = 0;
SwipeRefreshLayout swipejobs_list;
public MyJobs() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_jobs, container, false);
button = view.findViewById(R.id.createjob_button);
myjobsview = view.findViewById(R.id.my_jobs_recycler);
swipejobs_list = view.findViewById(R.id.swipejobs_list);
myjobslist = new ArrayList<>();
myjobsview.setHasFixedSize(true);
Myjobsviewmodel model = ViewModelProviders.of(getActivity()).get(Myjobsviewmodel.class);
myjobsadapter = new MyJobsAdapter(getActivity());
model.getMoviesInTheaterList().observe(this, new Observer<PagedList<MyJobspojo.Job>>() {
@Override
public void onChanged(PagedList<MyJobspojo.Job> jobs) {
Toast.makeText(getActivity(),"error"+jobs,Toast.LENGTH_LONG).show();
myjobsadapter.submitList(jobs);
myjobsview.setLayoutManager(new LinearLayoutManager(getActivity()));
myjobsview.setAdapter(myjobsadapter);
myjobsadapter.notifyDataSetChanged();
}
});
swipejobs_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
model.refreshmyjobs();
if (swipejobs_list.isRefreshing()) {
swipejobs_list.setRefreshing(false);
}
}
});
return view;
public class MyJobsDataSource extends PageKeyedDataSource<Integer, My
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
}
});
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, MyJobspojo.Job> callback) {
Call<MyJobspojo> call = MyApplication.getSerivce().callMyjobs(String.valueOf(params.key), String.valueOf(10),"Active","MyJobs");
call.enqueue(new Callback<MyJobspojo>() {
@Override
public void onResponse(Call<MyJobspojo> call, Response<MyJobspojo> response) {
Integer adjacentKey = (params.key > 1) ? params.key - 1 : null;
if (response.body() != null) {
callback.onResult(response.body().getJobs(), adjacentKey);
Log.d("Response", String.valueOf(response.body()));
}
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
}
});
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, MyJobspojo.Job> callback) {
Call<MyJobspojo> call = MyApplication.getSerivce().callMyjobs(String.valueOf(params.key), String.valueOf(per_page), "Active", "MyJobs");
call.enqueue(new Callback<MyJobspojo>() {
@Override
public void onResponse(Call<MyJobspojo> call, Response<MyJobspojo> response) {
if (response.body() != null) {
callback.onResult(response.body().getJobs(), params.key+1);
// Log.d("PARAMS", String.valueOf(params.key+1));
Log.d("Response", String.valueOf(response.body()));
}
else {
}
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
Log.d("ERRORafter",t.toString());
}
});
}
}
public class MyJobsDataSource extends PageKeyedDataSource<Integer, MyJobspojo.Job> {
public static final int per_page = 10;
public static int page = 1;
public static final String status = "Active";
public static final String type = "MyJobs";
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, MyJobspojo.Job> callback) {
Call<MyJobspojo> call = MyApplication.getSerivce().callMyjobs(String.valueOf(page), String.valueOf(per_page), "Active", "MyJobs");
call.enqueue(new Callback<MyJobspojo>() {
@Override
public void onResponse(Call<MyJobspojo> call, Response<MyJobspojo> response) {
if (response.body() != null && response.body().getJobs()!=null) {
if (response.body().getJobs().size()>0){
callback.onResult(response.body().getJobs(), null, page+1);
}else {
Log.d("Response", String.valueOf(response.body().getMessage()));
}
}
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
}
});
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, MyJobspojo.Job> callback) {
Call<MyJobspojo> call = MyApplication.getSerivce().callMyjobs(String.valueOf(params.key), String.valueOf(10),"Active","MyJobs");
call.enqueue(new Callback<MyJobspojo>() {
@Override
public void onResponse(Call<MyJobspojo> call, Response<MyJobspojo> response) {
Integer adjacentKey = (params.key > 1) ? params.key - 1 : null;
if (response.body() != null) {
callback.onResult(response.body().getJobs(), adjacentKey);
Log.d("Response", String.valueOf(response.body()));
}
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
}
});
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, MyJobspojo.Job> callback) {
Call<MyJobspojo> call = MyApplication.getSerivce().callMyjobs(String.valueOf(params.key), String.valueOf(per_page), "Active", "MyJobs");
call.enqueue(new Callback<MyJobspojo>() {
@Override
public void onResponse(Call<MyJobspojo> call, Response<MyJobspojo> response) {
if (response.body() != null) {
callback.onResult(response.body().getJobs(), params.key+1);
// Log.d("PARAMS", String.valueOf(params.key+1));
Log.d("Response", String.valueOf(response.body()));
}
else {
}
}
@Override
public void onFailure(Call<MyJobspojo> call, Throwable t) {
Log.d("ERRORafter",t.toString());
}
});
}
}