title: How to query from Firebase
body: <p>I have been doing a lot of searching and I cannot figure out how to do a simple query from firebase. Here is the structure of my db:</p>
  
  <p><a href="https://i.stack.imgur.com/TG3wu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TG3wu.png" alt="Database"></a></p>
  
  <p>The parent of "locations" is the name of the database. I am trying to just get a List of all the children of "locations" Here is what is not working for me:</p>
  
  <pre><code> mFirebase = FirebaseDatabase.getInstance().getReference();
      //Create location list
      mFirebase.child("locations").addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              for(DataSnapshot entry: dataSnapshot.getChildren()) {
                  Log.d("HERE: ", entry.toString());
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  </code></pre>
  
  <p>The Log.d method is never ran, so it is never entering the forEach loop. Is there an easier way to query from the database, or am I doing this wrong? Thanks</p>
  
  <p>EDIT: Changed to addListenerForSingleValueEvent() and added a line before the ForEach loop to be sure that the onDataChange() method is actually being called (which it is)</p>
  
  <pre><code>mFirebase.child("locations").addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              Log.d("Entered: ", "onDataChange");
              for(DataSnapshot entry: dataSnapshot.getChildren()) {
                  Log.d("HERE: ", entry.toString());
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  </code></pre>
  
  <p>Here is the entire code: (Note: I am switching from SQLite to firebase, so there is still code from SQLite there, that will be gone when I get Firebase working. </p>
  
  <pre><code>package osu.mobile_apps.ohiostatetourchallenge;
  
  import android.content.Intent;
  import android.provider.ContactsContract;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.*;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  import database.OsuTourDbSchema.DatabaseHelper;
  
  public class ListActivity extends AppCompatActivity {
  
  private RecyclerView mlocations;
  private RecyclerView.Adapter mAdapter;
  private List&lt;ListItem&gt; listItems;
  private DatabaseHelper mDatabaseHelper = new DatabaseHelper(this);
  private DatabaseReference mFirebase;
  
  // Stops the back button
  @Override
  public void onBackPressed() {}
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.d("TESTING","ON CREATE() FOR LIST");
      setContentView(R.layout.activity_list);
  
      //Get username from log in activity
      //String userName = getIntent().getExtras().getString("User");
  
      TextView welcome = (TextView) findViewById(R.id.welcome);
      if(welcome != null) {
          //welcome.setText("Welcome " + userName + "!");
      }
      mlocations = (RecyclerView) findViewById(R.id.locationList);
      //Every item has fixed size
      mlocations.setHasFixedSize(true);
      mFirebase = FirebaseDatabase.getInstance().getReference();
      //Create location list
      mFirebase.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              Log.d("Before Loop: ", dataSnapshot.toString());
              for(DataSnapshot entry: dataSnapshot.getChildren()) {
                  Log.d("HERE: ", entry.getValue().toString());
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
      mlocations.setLayoutManager(new LinearLayoutManager(this));
      List&lt;Location&gt; locations = mDatabaseHelper.getLocations();
      Collections.sort(locations, Location.LocationNameComparator);
      listItems = new ArrayList&lt;&gt;();
  
      String description;
      String smallDescription;
  
      for(Location entry: locations) {
          description = entry.getDescription();
          //TODO Check if location is unlocked or not
          if (description.length()&gt;110){
              smallDescription = description.substring(0,110) + "...";
          } else {
              smallDescription = description;
          }
          ListItem item = new ListItem(entry.getName(), smallDescription);
          listItems.add(item);
      }
  
      mAdapter = new myAdpater(listItems);
  
      mlocations.setAdapter(mAdapter);
  
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu){
      getMenuInflater().inflate(R.menu.menu_list,menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item){
      switch(item.getItemId()){
          case R.id.map:
              //Start map activity
  
              return true;
  
          case R.id.about:
              //Start about activity
              Intent intent = new Intent(ListActivity.this, AboutActivity.class);
              startActivity(intent);
              return true;
  
      }
      return super.onOptionsItemSelected(item);
  }
  
  public void onClick(View v){
      Intent intent = new Intent(ListActivity.this, InformationActivity.class);
      if(v.getId()==R.id.textViewHead){
          TextView TestView = (TextView) v.findViewById(R.id.textViewHead);
          intent.putExtra("Header", TestView.getText());
      }
      startActivity(intent);
  }
  
  }
  </code></pre>