title: Passing query parameters to LiveData/Viewmodel in Android app
body: <p>Since I'm new to programming Android apps I followed a tutorial on how to use the Android Architecture Components and Firebase for implementing the MVVM (using LiveData, ViewModel, etc.).</p>
  <p>The tutorial I followed can be found here:</p>
  <ul>
  <li><a href="https://evanamargain.com/android/android-livedata-and-viewmodel-in-java-tutorial/" rel="nofollow noreferrer">Part 1</a></li>
  <li><a href="https://evanamargain.com/android/firebase-firestore-for-android-architecture-components/" rel="nofollow noreferrer">Part 2</a></li>
  <li><a href="https://evanamargain.com/android/firestore-with-android-architecture-components-integration/" rel="nofollow noreferrer">Part 3</a></li>
  </ul>
  <p>I'm now left with what I think is a decent implementation of the MVVM, but I can not wrap my head around how I am supposed to pass query parameters to it. Right now I need to hardcode the ID of the document I want to retrieve:</p>
  <pre><code>public class AlarmDAO {
  
      private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
  
      public AlarmLiveData getFirestoreLiveData() {
          DocumentReference documentReference = firebaseFirestore.collection(Collection.ALARMS.name).document(&quot;5RxJNuNyhDJlz49wpBkw&quot;);
  
          return new AlarmLiveData(documentReference);
      }
  
  }
  </code></pre>
  <p>That then gets called by a class extending <code>ViewModel</code>.</p>
  <pre><code>public class AlarmViewModel extends ViewModel {
  
      private AlarmDAO DAO = new AlarmDAO();
      private AlarmLiveData liveData = null;
  
      public LiveData&lt;Alarm&gt; getAlarmLiveData() {
          liveData = DAO.getFirestoreLiveData();
          return liveData;
      }
  
      public LiveData&lt;Alarm&gt; getAlarm() {
          return liveData.alarm;
      }
  
  }
  </code></pre>
  <p>And then I observe that data in my activity:</p>
  <pre><code>model.getAlarmLiveData().observe(this, Observable -&gt; {});
  model.getAlarm().observe(this, alarm -&gt; {
     if (alarm != null) {
        alarmTextView.setText(alarm.getTest());
     else {
        Log.d(TAG, &quot;Waiting for data&quot;);
     }
  });
  </code></pre>
  <p>My problem is that I do not see a way of querying for a specific alarm. For instance <code>model.getAlarm(&quot;someId&quot;)</code>. I am under the impression that it should be done in the DAO and/or the ViewModel, but I can't figure out how. Another thing I do not understand is why I need to observe both <code>model.getAlarmLiveData()</code> and <code>model.getAlarm()</code> in my activity, as using only one does not work. The answer to both of those questions is most likely very simple, but thus far I haven't been able to figure it out.</p>
  <p>For completeness: the <code>Alarm</code> class is nothing besides a getter and setter for two strings, and the <code>AlarmLiveData</code> class is below.</p>
  <pre><code>public class AlarmLiveData extends LiveData&lt;Alarm&gt; implements EventListener&lt;DocumentSnapshot&gt; {
  
      private static final String TAG = AlarmLiveData.class.getSimpleName();
  
      private Alarm alarmTemp = new Alarm();
      private DocumentReference documentReference;
      private ListenerRegistration listenerRegistration = () -&gt; {};
  
      public MutableLiveData&lt;Alarm&gt; alarm = new MutableLiveData&lt;&gt;();
  
      public AlarmLiveData(DocumentReference documentReference) {
          this.documentReference = documentReference;
      }
  
      @Override
      protected void onActive() {
          listenerRegistration = documentReference.addSnapshotListener(this);
          super.onActive();
      }
  
      @Override
      protected void onInactive() {
          listenerRegistration.remove();
          super.onInactive();
      }
  
  
      @Override
      public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
          if (documentSnapshot != null &amp;&amp; documentSnapshot.exists()) {
              alarmTemp = new Alarm();
              alarmTemp.setId(documentSnapshot.getId());
              alarmTemp.setTest(documentSnapshot.get(&quot;test&quot;).toString());
              alarm.setValue(alarmTemp);
          } else {
              Log.d(TAG, &quot;ERROR&quot;);
          }
      }
  }
  </code></pre>
  <p>Thank you for reading, I'm looking forward to the answer(s)!</p>