title: How can I observe LiveData and then pass this data to my adapter?
body: <p>I have a <a href="https://i.stack.imgur.com/PtMzc.png" rel="nofollow noreferrer">Calendar</a>.
  To create this I have a <code>CalendarFragment</code> which opens <code>CustomCalendarView</code></p>
  
  <p>(a class which extends LinearLayout).</p>
  
  <p>This then uses <code>MyGridAdapter</code> (class which extends ArrayAdapter) to construct the calendar.</p>
  
  <p>When you click a cell on the calendar, you are taken to a new activity in which you can save a log containing some info (and the date of the cell in the calendar which was clicked).</p>
  
  <p>I have a query in my Dao class: <code>Log_Entries_Dao</code> which is passed to <code>LogEntriesRepository</code> and then <code>LogEntriesViewModel</code>.</p>
  
  <p><strong>The Query:</strong> </p>
  
  <p><code>@Query("SELECT date FROM log_entries_table WHERE log_entries_table.date = :date " )
      LiveData&lt;List&lt;LogDates&gt;&gt; getAllDatesWithLogs(List&lt;Date&gt; date);</code></p>
  
  <p>The query receives a list of all dates of the calendar month and then returns a list of all the dates in which a logEntry is present.</p>
  
  <p>Now i would like to observe this LiveData in my <code>setupCalendar</code> method and pas the list of dates in which a logEntry is present to my <code>gridAdater</code> class so I can add circles to all of the cells in which a logEntry is present.</p>
  
  <p>The trouble is, I believe that it is bad practice to use my <code>LogViewModel</code> class inside <code>CustomCalendarView</code> (LinearLayout class).
  How could  correctly query my database, observe the liveData and send this to my adapter from inside my setUpCalendar method?</p>
  
  <p><strong>Calendar Fragment</strong></p>
  
  <pre><code>public class CalendarFragment extends Fragment {
  
      CustomCalendarView customCalendarView;
      List&lt;Date&gt; dates = new ArrayList&lt;&gt;();
      LogEntriesViewModel logEntriesViewModel;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.calendar_activity_main, container,false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          customCalendarView =(CustomCalendarView) getView().findViewById(R.id.custom_calendar_view);
          ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
      }
  }
  
  </code></pre>
  
  <p><strong>CustomCalendarView</strong> </p>
  
  <pre><code>
  public class CustomCalendarView extends LinearLayout {
      ImageButton NextButton, PreviousButton;
      TextView CurrentDate;
      GridView gridView;
      public static final int MAX_CALENDAR_DAYS = 42;
      Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
      Context context;
      MyGridAdapter myGridAdapter;
      SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
      SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.ENGLISH);
      SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.ENGLISH);
      SimpleDateFormat eventDateFormat = new SimpleDateFormat(("dd-MM-yyyy"), Locale.ENGLISH);
  
      public static final String MY_PREFS_NAME = "MyPrefsFile";
  
      List&lt;Date&gt; dates = new ArrayList&lt;&gt;();
      List&lt;Log_Entries&gt; eventsList = new ArrayList&lt;&gt;();
  
      public CustomCalendarView(Context context) {
          super(context);
      }
  
      public CustomCalendarView(final Context context, @Nullable AttributeSet attrs) {
          super(context, attrs);
          this.context = context;
          InitializeLayout();
          SetUpCalendar();
  
          PreviousButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  calendar.add(Calendar.MONTH, -1);
                  SetUpCalendar();
              }
          });
  
          NextButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  calendar.add(Calendar.MONTH, 1);
                  SetUpCalendar();
              }
          });
  
  
          gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(context);
                  builder.setCancelable(true);
  
                  final String date = eventDateFormat.format(dates.get(position));
  
  
                  Intent i = new Intent(getContext(), WorkoutButtonsActivity.class);
                  i.putExtra(WorkoutButtonsActivity.EXTRA_DATE, date);
                  getContext().startActivity(i);
              }
          });
      }
  
      public CustomCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
      }
  
      private void InitializeLayout() {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(R.layout.calendar_layout, this);
          NextButton = view.findViewById(R.id.nextBtn);
          PreviousButton = view.findViewById(R.id.previousBtn);
          CurrentDate = view.findViewById(R.id.current_Date);
          gridView = view.findViewById(R.id.gridview);
      }
  
      private void SetUpCalendar() {
  
          String currentDate = dateFormat.format(calendar.getTime());
          CurrentDate.setText(currentDate);
          dates.clear();
          Calendar monthCalendar = (Calendar) calendar.clone();
          monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
          int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) - 1;
          monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);
  
          while (dates.size() &lt; MAX_CALENDAR_DAYS) {
              dates.add(monthCalendar.getTime());
              monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
  
  
          }
          myGridAdapter = new MyGridAdapter(context, dates, calendar, eventsList);
          gridView.setAdapter(myGridAdapter);
  
      }
  }
  
  </code></pre>
  
  <p><strong>MyGridAdapter</strong></p>
  
  <pre><code>
  public class MyGridAdapter extends ArrayAdapter {
      List&lt;Date&gt; dates;
      Calendar currentDate;
  
      List&lt;Log_Entries&gt; logs;
  
      LayoutInflater inflater;
  
      public MyGridAdapter(@NonNull Context context, List&lt;Date&gt; dates, Calendar currentDate, List&lt;Log_Entries&gt; logs){
          super(context, R.layout.single_cell_layout);
  
          this.dates = dates;
          this.currentDate = currentDate;
          this.logs = logs;
          inflater = LayoutInflater.from(context);
  
          Log.i("dates", String.valueOf(dates));
  
      }
      @NonNull
      @Override
      public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
          Date monthDate = dates.get(position);
          Calendar dateCalendar = Calendar.getInstance();
          dateCalendar.setTime(monthDate);
          int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
          int displayMonth = dateCalendar.get(Calendar.MONTH) +1;
          int displayYear = dateCalendar.get(Calendar.YEAR);
          int currentMonth = currentDate.get(Calendar.MONTH) + 1;
          int currentYear = currentDate.get(Calendar.YEAR);
          int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
  
  
  
          View view = convertView;
          if (view == null){
              view = inflater.inflate(R.layout.single_cell_layout, parent,false);
          }
  
          if (displayMonth == currentMonth &amp;&amp; displayYear == currentYear){
              view.setBackgroundColor(getContext().getResources().getColor(R.color.green));
          }
          else{
              view.setBackgroundColor(Color.parseColor("#cccccc"));
          }
  
          TextView Day_Number = view.findViewById(R.id.calendar_day);
          Day_Number.setText(String.valueOf(DayNo));
  
          Calendar eventCalendar = Calendar.getInstance();
  
          if(DayNo == currentDay &amp;&amp; displayMonth == eventCalendar.get(Calendar.MONTH) + 1 &amp;&amp; displayYear == eventCalendar.get(Calendar.YEAR)){
              Day_Number.setTextColor(Color.parseColor("#FFFF33"));
  
          }
  
  
          ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; logs.size(); i++){
          }
          return view;
      }
  
      @Override
      public int getCount() {
          return dates.size();
      }
  
      @Override
      public int getPosition(@Nullable Object item) {
          return dates.indexOf(item);
      }
  
      @Nullable
      @Override
      public Object getItem(int position) {
          return dates.get(position);
      }
  }
  
  </code></pre>
  
  <p><strong>LogEntriesRepository</strong></p>
  
  <pre><code>
  public class LogEntriesRepository {
  
      private Log_Entries_Dao log_entries_dao;
      private LiveData&lt;List&lt;Log_Entries&gt;&gt; allLogEntries;
      private LiveData&lt;List&lt;Log_Entries&gt;&gt; allWorkoutLogEntries;
  
      private LiveData&lt;List&lt;LogDates&gt;&gt; allDatesWithLog;
  
      public LogEntriesRepository(Application application) {
          ExerciseDatabase database = ExerciseDatabase.getInstance(application);
          log_entries_dao = database.log_entries_dao();
          allLogEntries = log_entries_dao.getAllFromLogEntries();
      }
  
      public void insert(Log_Entries log_entries) {
          new InsertLogEntryAsyncTask(log_entries_dao).execute(log_entries);
      }
  
      public void update(Log_Entries log_entries) {
          new UpdateLogEntryAsyncTask(log_entries_dao).execute(log_entries);
      }
  
      public void delete(Log_Entries log_entries) {
          new DeleteLogEntryAsyncTask(log_entries_dao).execute(log_entries);
      }
  
  
      public LiveData&lt;List&lt;Log_Entries&gt;&gt; getAllLogEntries() {
          return allLogEntries;
      }
  
      public LiveData&lt;List&lt;Log_Entries&gt;&gt; getAllWorkoutLogEntries(int junctionID, String date) {
          allWorkoutLogEntries = log_entries_dao.getAllFromWorkoutLogEntries(junctionID, date);
          return allWorkoutLogEntries;
      }
  
      public LiveData&lt;List&lt;LogDates&gt;&gt; getAllDateLogEntries(List&lt;String&gt; date) {
          allDatesWithLog = log_entries_dao.getAllDatesWithLogs(date);
          return allDatesWithLog;
      }
  
      private static class InsertLogEntryAsyncTask extends AsyncTask&lt;Log_Entries, Void, Void&gt; {
          private Log_Entries_Dao log_entries_dao;
  
          private InsertLogEntryAsyncTask(Log_Entries_Dao log_entries_dao) {
              this.log_entries_dao = log_entries_dao;
          }
  
          @Override
          protected Void doInBackground(Log_Entries... log_entries) {
              log_entries_dao.insert(log_entries[0]);
              return null;
          }
      }
  
      private static class UpdateLogEntryAsyncTask extends AsyncTask&lt;Log_Entries, Void, Void&gt; {
          private Log_Entries_Dao log_entries_dao;
  
          private UpdateLogEntryAsyncTask(Log_Entries_Dao log_entries_dao) {
              this.log_entries_dao = log_entries_dao;
          }
  
          @Override
          protected Void doInBackground(Log_Entries... log_entries) {
  
              log_entries_dao.update(log_entries[0]);
              return null;
          }
      }
  
      private static class DeleteLogEntryAsyncTask extends AsyncTask&lt;Log_Entries, Void, Void&gt; {
          private Log_Entries_Dao log_entries_dao;
  
          private DeleteLogEntryAsyncTask(Log_Entries_Dao log_entries_dao) {
              this.log_entries_dao = log_entries_dao;
          }
  
          @Override
          protected Void doInBackground(Log_Entries... log_entries) {
              log_entries_dao.delete(log_entries[0]);
              return null;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>LogEntriesViewModel</strong></p>
  
  <pre><code>public class LogEntriesViewModel extends AndroidViewModel {
  
      private LogEntriesRepository repository;
      private LiveData&lt;List&lt;Log_Entries&gt;&gt; allLogEntries;
      private LiveData&lt;List&lt;Log_Entries&gt;&gt; allWorkoutLogEntries;
  
      private LiveData&lt;List&lt;LogDates&gt;&gt; allDateLogEntries;
  
  
      public LogEntriesViewModel(@NonNull Application application) {
          super(application);
          repository = new LogEntriesRepository(application);
          allLogEntries = repository.getAllLogEntries();
      }
  
  
      public void insert(Log_Entries log_entries){
          repository.insert(log_entries);
      }
      public void update(Log_Entries log_entries){
          repository.update(log_entries);
      }
      public void delete(Log_Entries log_entries ) {
          repository.delete(log_entries);
      }
     // public LiveData&lt;List&lt;Log_Entries&gt;&gt; getAllLogs(){
     //     return allLogEntries;
     // }
  
      public LiveData&lt;List&lt;Log_Entries&gt;&gt; getAllWorkoutLogEntries(int junctionID, String date){
          allWorkoutLogEntries = repository.getAllWorkoutLogEntries(junctionID, date);
          return allWorkoutLogEntries;
      }
  
      public LiveData&lt;List&lt;LogDates&gt;&gt; getAllDateLogEntries(List&lt;String&gt; date){
          allDateLogEntries = repository.getAllDateLogEntries(date);
          return allDateLogEntries;
      }
  
  }
  
  </code></pre>