title: In Android Room Can I loop through all Records
body: <p>Hi Guys so I checked the Android tutorial for Room Sqlite which is nice but I am facing a small problem.</p>
  
  <p>I created a table with two columns Keys and Values (both strings).</p>
  
  <pre><code>@Entity(tableName = "key_table")
  public class Key {
  @PrimaryKey(autoGenerate = true)
  private int id;
  private String key;
  private String value;
  //...Setters and getters and contructor 
  </code></pre>
  
  <p>With regards to the <a href="https://developer.android.com/topic/libraries/architecture/room" rel="nofollow noreferrer">Google docs</a> I made several files : The Entity (class), The Dao, The Database, The Repository, The ViewModel the adapter (recycler) for listview . I was Happy to see everything working with an Activity/ Fragment.</p>
  
  <p><strong>Now The Problem : Is it possible to do something simple like Get all the rows KEYS &amp; VALUES and loop through them??</strong></p>
  
  <p>Now In the Dao File I have:</p>
  
  <pre><code>@Query("SELECT * FROM key_table ORDER BY id DESC")
  LiveData&lt;List&lt;Key&gt;&gt; getAllkeys();
  </code></pre>
  
  <p>I tried to think of a way to loop through this LiveData List but the only thing that I found was setting an observer and waiting for a change and setting adapter clas for ListView.</p>
  
  <p>I tried adding something like this in the KeyDao.java file:</p>
  
  <pre><code>@Query("SELECT * FROM key_table ORDER BY id DESC")
  List&lt;Key&gt; getKeys();
  </code></pre>
  
  <p>also I have KeyRepository.java where I added some code to call the getKeys(); that I added in the Dao</p>
  
  <pre><code>public class KeyRepository {
      private KeyDao keyDao;
      private LiveData&lt;List&lt;Key&gt;&gt; allKeys;
      private List&lt;Key&gt; TheKeys;
      public KeyRepository(Application application) {
        KeyDatabase database = KeyDatabase.getInstance(application);
        keyDao = database.keyDao();
        allKeys = keyDao.getAllkeys();
        TheKeys = keyDao.getKeys();
      }
  
      public void insert(Key key) {
        new InsertKeyAsyncTask(keyDao).execute(key);
      }
  
      public void update(Key key) {
        new UpdateKeyAsyncTask(keyDao).execute(key);
      }
  
      public void delete(Key key) {
        new DeleteKeyAsyncTask(keyDao).execute(key);
      }
  
      public void deleteAllKeys() {
        new DeleteAllKeysAsyncTask(keyDao).execute();
      }
  
      public LiveData&lt;List&lt;Key&gt;&gt; getAllKeys() {
        return allKeys;
      }
      //I ADDED THIS
      public List&lt;Key&gt;  getKeys() {
        return TheKeys;
      }
  // The implementation of the Asyncs ...
  </code></pre>
  
  <p>Ok So far I got an Error and Managed to make it work by Adding the .allowMainThreadQueries () in the DataBuilder following @Christilyn suggestion.</p>
  
  <p>Still the result is an Empty Array!! I am missing something ?
  with the changes to the Keyrepository.java file where I tried to add the getKeys() but I got an error, I was thinking of doing something like this on the activity or fragment :</p>
  
  <pre><code>keyViewModel = ViewModelProviders.of(this).get(KeyViewModel.class);
  List&lt;Key&gt;  keys = keyViewModel.getKeys();
  int s = keys.size();
  for(int i=0;i&lt;s; i++){
    Key k;
    k = keys.get(i);
    //... Now we should loop but the getKeys(); doesnt work !! maybe it's a wrong approach
  }
  </code></pre>
  
  <p><strong>So can we just loop through that LiveData which is suposed to encapsulate data without setting up an observer and waiting for a change?</strong></p>