我的片段中有一个带有AAC的RecyclerView。 ViewModel->存储库-> DAO,其中包含一些自定义查询和一个getAllItems。
我想使用过滤器FAB或微调器来调用getOrderItemList或getWhereItemList查询,但是我不知道该怎么做。
我有一个适用于SearchView的存储库过滤器,但情况有所不同,现在我想更改列表顺序(字母,年份...),并在对话框中创建一个带有很多复选框的WhereCondition(例如:我选中“完成”和“操作”复选框,并创建字符串whereCondition =“(status ='complete'and体裁如'%Action%')”)。
如何从Fragment调用getWhereItemList和getOrderItemList查询以更改RecyclerView内容?
ItemDAO:
@Query("SELECT * from item_table ")
<List<Item>> getItemList();
@Query("SELECT * from item_table ORDER by :order DESC")
<List<Item>> getOrderItemList(String order);
@Query("SELECT * from item_table WHERE :whereCondition")
<List<Item>> getWhereItemList(String whereCondition);
我的片段用getAllItems填充RecyclerView:
private ItemViewModel myItemViewModel;
RecyclerView myRecyclerView = findViewById(R.id.recyclerview);
final ItemListAdapter myAdapter = new ItemListAdapter(this);
myRecyclerView.setAdapter(myAdapter);
myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
myItemViewModel = ViewModelProviders.of(this).get(ItemViewModel.class);
myItemViewModel.getAllItems().observe(this, new Observer<List<Item>>() {
@Override
public void onChanged(@Nullable final List<Item> items) {
myAdapter.setItems(items);
}
ItemListAdapter:
private List<Item> myItems;
void setItems(List<Item> items){
myItems = items;
notifyDataSetChanged();
}
ItemViewModel:
private ItemRepository myRepository;
private LiveData<List<Item>> myAllItems;
public ItemViewModel (Application application) {
super(application);
myRepository = new ItemRepository(application);
myAllItems = myRepository.getAllItems();
}
LiveData<List<Item>> getAllItems() { return myAllItems; }
谢谢。
答案 0 :(得分:0)
这个想法是有两个LiveData
实例:
List<Item>
的人。这也应该对其他LiveData
的更改做出反应,并在必要时获取新的List<Item>
。您可以使用Transformations.SwitchMap来实现LiveData2
。它的作用是基本上返回一个LiveData
实例,该实例可以响应另一个LiveData
对象而切换到另一个源。
ItemViewModel:
private ItemRepository myRepository;
/**
* Keep track of the current filter type.
* In this example the initial value is set to Filter.ALL, which
* represents the non-filtered list.
*/
private MutableLiveData<Filter> itemFilter = new MutableLiveData<>(Filter.ALL);
/**
* Emits list of items
*/
private LiveData<List<Item>> myItems = Transformations.switchMap(itemFilter, filter -> {
// Everytime itemFilter emits a new value, this piece of code
// will be invoked. You are responsible for returning the
// LiveData instance according to the filter value.
switch(filter.type) {
case ALL:
return myRepository.getAllItems();
case ORDER_BY:
return myRepository.getOrderItemList(filter.query);
case WHERE:
return myRepository.getWhereItemList(filter.query);
}
});
public ItemViewModel (Application application) {
super(application);
myRepository = new ItemRepository(application);
}
public LiveData<List<Item>> getItems() { return myItems; }
/**
* View should call this method in order to switch to different
* filter.
*/
public void changeFilter(Filter itemFilter) {
this.itemFilter.setValue(filter);
}
定义此自定义过滤器类:
public class Filter {
public enum Type {
ALL,
ORDER_BY,
WHERE
}
final public Type type;
final public String query;
public Filter(Type type, String query) {
this.type = type;
this.query = query;
}
}