实际上,我在这里找不到任何错误。我想我在这里错过了一些东西。该过滤器按钮不起作用。您的帮助将不胜感激。谢谢
sortByLeastExpensive = rootView.findViewById(R.id.sort_by_least_expensive);
sortByMostExpensive = rootView.findViewById(R.id.sort_by_most_expensive);
sortByMostRecent = rootView.findViewById(R.id.sort_by_most_recent);
applyFiltersBtn = rootView.findViewById(R.id.apply_filters_btn);
sortByLeastExpensive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set the filter; or call the viewmodel to set the filter?
mViewModel.sortBy.setValue(getResources().getString(R.string.sort_rooms_ascending));
}
});
sortByMostExpensive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewModel.sortBy.setValue(getResources().getString(R.string.sort_rooms_descending));
}
});
sortByMostRecent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewModel.sortBy.setValue("sort_most_recent");
}
});
return rootView;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(getActivity()).get(FilterRoomsViewModel.class);
mViewModel.getSortBy().observe(this, Observer -> {
Log.i("Viewmodel", "Viewmodel ,sortby: " + mViewModel.sortBy.getValue());
});
applyFiltersBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FilterRoomsRepository roomsRepository = new FilterRoomsRepository();
roomsRepository.getFilteredRooms(mViewModel.sortBy.getValue());
}
});
}
我已经更新了我的viewmodel.class。现在,它向我显示了fragment.class的另一个错误
public FilterRoomsViewModel(Application application){
super();
FilterRoomsRepository filterRoomsRepository = new FilterRoomsRepository(application);
}
public MutableLiveData<String> getRooms(){
if (rooms==null){
rooms = new MutableLiveData<String>();
}
return rooms;
}
public LiveData<String> getSortBy() {
if (sortBy == null){
sortBy = new MutableLiveData<String>();
}
return sortBy;
}
if(sortyBy.equals("ascending")){
Log.i("Sort by", "Rooms sorted in ascending order");
filterQuery.orderByAscending("roomRentalAmount");
}else if (sortyBy.equals("descending")){
Log.i("Sort by", "Rooms sorted in descending order");
filterQuery.orderByDescending("roomRentalAmount");
}else {
filterQuery.orderByDescending("updatedAt");
}
filterQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
//success
Log.i("Rooms", "Filtered rooms, " + objects.get(0).getObjectId());
rooms.setValue(objects);
}else{
//error, handle accordingly
Log.e("Error", "Error filtering, " + e.getMessage());
}
}
});
Log.i("Rooms", "Live data rooms, " + rooms.getValue());
return rooms;
}
答案 0 :(得分:0)
它不显示什么意思?它没有显示在ui或日志中吗?因为当您调用getFilteredRooms时,对结果不执行任何操作。 我的建议是将存储库调用移至viewModel,以便与其在片段中调用getFilteredRooms,而不是像viewModel.getFilteredRooms这样调用sth,然后在viewModel中调用存储库,然后使用结果进行设置实时数据的价值。 然后,在片段中,您只需要观察此变量,然后根据该变量更新ui。通过这样做,您可以将逻辑(视图模型和存储库)与ui部件(片段)分开。
这是一个很好的教程,介绍如何使用mvvm模式进行编码:https://medium.com/@er.ankitbisht/mvvm-model-view-viewmodel-kotlin-google-jetpack-f02ec7754854