I have a Rooms database which stores flight information, and I want to retrieve a list of flights which are associated with a given username and userID. I am attaching herewith the code for my activity
Activity code
userViewModel.getAllUsers().observe(ReportListingActivity.this, new Observer<List<User>>() {
@Override
public void onChanged(List<User> users) {
user_details = getUsers();
user_name = user_details[0];
user_id = user_details[1];
}
});
flightViewModel.getAllFlights().observe(this, new Observer<List<Flight>>() {
@Override
public void onChanged(List<Flight> flight_list) {
if (flight_list.size() == 0) return;
for(int i = 0; i <flight_list.size();i++){
String flightnumber = flight_list.get(i).getFlightNumber();
String departuredate = flight_list.get(i).getDate();
String status = flight_list.get(i).getFlight_closure_status();
String user_name = flight_list.get(i).getName();
String user_id = flight_list.get(i).getStaffID();
reportitems.add(new ReportItem(flightnumber,departuredate,status, user_name, user_id));
}
mRecyclerView.getAdapter().notifyDataSetChanged();
flightViewModel.getAllFlights().removeObservers(ReportListingActivity.this);
}
});
From UserViewModel I get the userID, and from FlightViewModel I want to get the flights which are associated with that username and userID. I already have a username and userID attribute in the Flight_Table.
Below is the query I have written in FlightDao to get a flight given username and userID
FlightDao
@Query("SELECT * FROM flight_table WHERE (name = :name and staffID = :id)")
public Flight getFlightReports(String name, String id);
I want to display all the flights that are associated with the given name and id. How can I achieve that?