I have a RecyclerView with an AAC in my Fragment. ViewModel --> Repository --> DAO with some custom Queries and a getAllItems.
I want to use a Filter FAB or a Spinner to call getOrderItemList or getWhereItemList queries but i dont know how must i do it.
I have a Repository Filter for my SearchView but is a different thing, now i want to change list order (alphabetical, year...) and create a WhereCondition with a lot of checkbox that i have in a Dialog (example: i check "complete" and "Action" checkbox and creates the String whereCondition = "(status = 'complete' and genre like '%Action%')" ).
How can i call getWhereItemList and getOrderItemList queries from my Fragment to change my RecyclerView content?
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);
My Fragment fills the RecyclerView with getAllItems:
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; }
Thanks.
> myAllItems? 2 days and nothing. Maybe in the third iwill be lucky.... This night i will do another Test and I share the final code with you. Yesterday i did change a lot of thing for nothing. Its a nightmare.... T_T
– Glasindor May 03 '19 at 07:05> getOrderItems(String order); When i pass order = name, order = lenght.... i always get Order By ID_Item. Them the RecyclerView is always the same. With queries without params is OK. Then how can i use a query with param?
– Glasindor May 03 '19 at 22:15> getItemsQuery(SupportSQLiteQuery query); I have the query "SELECT * FROM item_table ORDER BY ? ASC"
– Glasindor May 03 '19 at 23:00