2

I have a getAllFlights() method in my ViewModel class which is supposed to return all the flights in my rooms database, however, when I call the method in my activity, it returns null even though I have flights existing in the database. Below is the code for my activity where I am calling flightViewModel.getAllFlights.getValue() method. I am using the MVVM model.

Activity code

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_listing);

        uploadAllBtn = findViewById(R.id.uploadAllReports);
        searchFlights = findViewById(R.id.searchFlightText);
        mRecyclerView = findViewById(R.id.recyclerView);


        flightViewModel = new ViewModelProvider(this).get(FlightViewModel.class);

        flightViewModel.getAllFlights().observe(this, new Observer<List<Flight>>() {
            @Override
            public void onChanged(List<Flight> flight_list) {

                String flightno = flight_list.get(1).getFlightNumber();
                String flightdate = flight_list.get(1).getDate();

                String[] flight_details = new String[2];
                flight_details[0]= flightno;
                flight_details[1] = flightdate;

                Log.v("pp", flight_details[0]);

                for(int i = 0; i <flight_list.size();i++){
                    String flightnumber = flight_list.get(i).getFlightNumber();
                    String departuredate = flight_list.get(i).getDate();

                    reportitems.add(new ReportItem(flightnumber,departuredate));

                    
                }
                flightViewModel.getAllFlights().removeObservers(ReportListingActivity.this);
            }
        });

        mLayoutManager = new LinearLayoutManager(ReportListingActivity.this);
        mAdapter = new ReportAdapter(reportitems, ReportListingActivity.this);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
  • Maybe add your ViewModel code, too. – JacksOnF1re Feb 18 '21 at 20:24
  • And there is no need to remove the observer. Even if it does not change. LiveData is 1. for async work (i.e db queries) and 2. for observing updates. Get the live data through your VM and your VM gets the liveData from the db. Make optional mapping to view items in the VM. – JacksOnF1re Feb 18 '21 at 20:31

1 Answers1

2

you should not use getValue() in case of observing the data you should use observe() method of your livedata.

update:

you can use this code:

flightViewModel.getAllFlights().observe(this, new Observer<List<Flight>>() {
    @Override
    public void onChanged(List<Flight> flight_list) {

        if (flight_list.size() == 0) return;
    
        String flightno = flight_list.get(0).getFlightNumber();
        String flightdate = flight_list.get(0).getDate();

        String[] flight_details = new String[2];
        flight_details[0]= flightno;
        flight_details[1] = flightdate;

        for(int i = 0; i <flight_list.size();i++){
            String flightnumber = flight_list.get(i).getFlightNumber();
            String departuredate = flight_list.get(i).getDate();

            reportitems.add(new ReportItem(flightnumber,departuredate));
        }
        mRecyclerView.getAdapter().notifyDataSetChanged()
        flightViewModel.getAllFlights().removeObservers(this);
    }
});
Hamid Sj
  • 983
  • 7
  • 19
  • I do not want to observe the data though, because the data is not changing on that particular activity. I just want to fetch the data from the database and show it in my recycler view – OutOfBounds Feb 18 '21 at 19:48
  • at last you must use `observe()` method. but you can remove the observer after first time that it observed a value. or I recommend to not using livedata if your data is not changing. your problem is because you are trying to get the value that stored in the livedata before that data fetched from database. – Hamid Sj Feb 18 '21 at 19:53
  • So how do I get the list of all flights in my database? Would you be able to modify the code above? – OutOfBounds Feb 18 '21 at 19:56
  • When I try adding the observe() method in the end, I have to forcefully add the Observer as well along with the OnChanged method which then returns me an empty list again since nothing changes on that particular activity – OutOfBounds Feb 18 '21 at 20:01
  • @OutOfBounds I updated my answer, check it now – Hamid Sj Feb 18 '21 at 20:06
  • I tried this, but it still does not show the flight list on my recycler view. It is empty – OutOfBounds Feb 18 '21 at 20:13
  • It can capture the flight information now but it does not show the list on the recycler view – OutOfBounds Feb 18 '21 at 20:14
  • is the list empty or just recyclerView not showing the list data? – Hamid Sj Feb 18 '21 at 20:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228913/discussion-between-outofbounds-and-hamid-sj). – OutOfBounds Feb 18 '21 at 20:18