Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import android.content.Context;
  4.  
  5. import java.util.Date;
  6.  
  7. import androidx.lifecycle.LiveData;
  8. import androidx.room.Room;
  9.  
  10. public class ForecastRepository {
  11.  
  12. private ForecastDao forecastDao;
  13.  
  14. private static ForecastRepository repo = new ForecastRepository();
  15. private ForecastDatabase database = null;
  16.  
  17. private ForecastRepository() {}
  18.  
  19. public static ForecastRepository getRepo(Context context) {
  20. repo.buildDatabase(context);
  21. return repo;
  22. }
  23.  
  24. private void buildDatabase(Context context) {
  25. if (database == null) {
  26. database = Room.databaseBuilder(context.getApplicationContext(), ForecastDatabase.class, "forecast_database").build();
  27. forecastDao = database.forecastDao();
  28. Forecast test = new Forecast();
  29. test.setDescription("Brussel");
  30. test.setTemperature(69);
  31. test.setLastUpdated(new Date("11/12/2018"));
  32. database.forecastDao().insertAll();
  33. }
  34. }
  35.  
  36. public LiveData<Forecast> getForecast() {
  37. if (database == null) {
  38. return null;
  39. }
  40. return forecastDao.getOne();
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement