Я новичок в android. У меня есть проект MVVM. Я привык к базе данных, LiveData, recyclerView. Я хочу заняться фильтрацией данных таблиц. Я понятия не имею. Что мне делать? Это мой Dao
@Dao
public interface ReceiptDao {
@Insert
void insert(Receipt receipt);
@Query("SELECT Receipt FROM Receipt " +
"WHERE Person.removed = 0 AND Receipt.personId = :personId")
LiveData<List<Receipt>> findByPersonId(int personId);
...
и этот репозиторий
public class ReceiptRepository {
private ReceiptDao receiptDao;
private LiveData<List<Receipt>> receiptListLiveData;
public ReceiptRepository(Application application, int personId) {
DataBase dataBase = DataBase.getDataBase(application);
receiptDao = dataBase.receiptDao();
receiptListLiveData = dataBase.receiptDao().findByPersonId(personId);
}
public LiveData<List<Receipt>> getReceiptListLiveData() {
return receiptListLiveData;
}
public void insert(Receipt receipt) {
new InsertAsyncTask(receiptDao).execute(receipt);
}
public void update(Receipt receipt) {
new UpdateAsyncTask(receiptDao).execute(receipt);
}
/**/
private static class InsertAsyncTask extends AsyncTask<Receipt, Void, Void> {
private ReceiptDao receiptDao;
private InsertAsyncTask(ReceiptDao receiptDao) {
this.receiptDao = receiptDao;
}
@Override
protected Void doInBackground(Receipt... receipts) {
receiptDao.insert(receipts[0]);
return null;
}
}
...
ViewModel
public class ReceiptViewModel extends AndroidViewModel {
private ReceiptRepository repository;
private final LiveData<List<Receipt>> receiptListLiveData;
public ReceiptViewModel(@NonNull Application application, int personId) {
super(application);
repository = new ReceiptRepository(application, personId);
receiptListLiveData = repository.getReceiptListLiveData();
}
public LiveData<List<Receipt>> getReceiptListLiveData() {
return receiptListLiveData;
}
public void insert(Receipt receipt) {
repository.insert(receipt);
}
...
просмотр модели фабрики
public class ReceiptViewModelFactory implements ViewModelProvider.Factory {
private Application application;
private int personId;
public ReceiptViewModelFactory(Application application, int personId) {
this.application = application;
this.personId = personId;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new ReceiptViewModel(application, personId);
}
}
адаптер
public class ReceiptAdapter extends RecyclerView.Adapter<ReceiptAdapter.ReceiptHolder> {
List<Receipt> receiptList = new ArrayList<>();
private View.OnLongClickListener longClickListener;
public ReceiptAdapter(View.OnLongClickListener longClickListener) {
this.longClickListener = longClickListener;
}
@NonNull
@Override
public ReceiptHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext())
.inflate(R.layout.receipt_item, parent, false);
return new ReceiptHolder(viewItem);
}
@Override
public void onBindViewHolder(@NonNull ReceiptHolder holder, int position) {
...
}
public void setReceiptList(List<Receipt> receiptList) {
this.receiptList = receiptList;
notifyDataSetChanged();
}
/**/
static class ReceiptHolder extends RecyclerView.ViewHolder{
...
}
...
может выполнять действие
...
recyclerViewAdapter = new ReceiptAdapter(this);
recyclerView = findViewById(R.id.recycler_view_recipe);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerViewAdapter);
viewModel = ViewModelProviders.of(this,
new ReceiptViewModelFactory(this.getApplication(), driver.getPersonId()))
.get(ReceiptViewModel.class);
viewModel.getReceiptListLiveData().observe(ReceiptActivity.this, new Observer<List<Receipt>>() {
@Override
public void onChanged(List<Receipt> receiptList) {
recyclerViewAdapter.setReceiptList(receiptList);
}
});
...
Квитанция - это одна из моих сущностей со свойствами, такими как Id, receitNumber, date, .... У меня есть другая операция, которую я получаю значения для фильтра Данные таблицы квитанций и отправил их вышеупомянутой операции. Я не знаю, как создать динамический запрос или любой другой способ получить указанную c форму базы данных, и не имею ни малейшего представления, как отобразить их в recyclerView.