title: Android - Retrieve data from Firebase into Listview on Activity Load
body: <p>I am having a tough time figuring this out. I have an activity with a listview on it. My listview uses a row_item that contains a TextView and an imageView. </p>
  
  <p>Here is my Firebase RealTime database.
  <a href="https://i.stack.imgur.com/WL1Gq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WL1Gq.png" alt="Firebase Database"></a></p>
  
  <p>Here is my data object</p>
  
  <pre><code>public class Spacecraft
  {
      private int level;
      private int NumCorrect;
  
      public Spacecraft()
      {
      }
  
      public int getLevel() {return level;}
      public void setLevel(int level) {this.level = level;}
  
      public int getNumCorrect() {return NumCorrect;}
      public void setNumCorrect(int numCorrect) {NumCorrect = numCorrect;}
  }
  </code></pre>
  
  <p>My FireBaseHelper class</p>
  
  <pre><code>public class FirebaseHelper {
      DatabaseReference db;
      Boolean saved;
      ArrayList&lt;Spacecraft&gt; spacecrafts=new ArrayList&lt;&gt;();
      /*
   PASS DATABASE REFRENCE
    */
      public FirebaseHelper(DatabaseReference db) {
          this.db = db;
      }
      //WRITE IF NOT NULL
      public Boolean save(Spacecraft spacecraft)
      {
          if(spacecraft==null)
          {
              saved=false;
          }else
          {
              try
              {
                  db.child("users").push().setValue(spacecraft);
                  //db.child("Spacecraft").push().setValue(spacecraft);
                  saved=true;
              }catch (DatabaseException e)
              {
                  e.printStackTrace();
                  saved=false;
              }
          }
          return saved;
      }
      //IMPLEMENT FETCH DATA AND FILL ARRAYLIST
      private void fetchData(DataSnapshot dataSnapshot)
      {
          spacecrafts.clear();
          for (DataSnapshot ds : dataSnapshot.getChildren())
          {
              Spacecraft spacecraft=ds.getValue(Spacecraft.class);
              spacecrafts.add(spacecraft);
          }
      }
      //RETRIEVE
      public ArrayList&lt;Spacecraft&gt; retrieve()
      {
          db.addChildEventListener(new ChildEventListener() {
              @Override
              public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot);
              }
              @Override
              public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot);
              }
              @Override
              public void onChildRemoved(DataSnapshot dataSnapshot) {
              }
              @Override
              public void onChildMoved(DataSnapshot dataSnapshot, String s) {
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
              }
          });
          return spacecrafts;
      }
  }
  </code></pre>
  
  <p>My Custom Adapter</p>
  
  <pre><code>public class CustomAdapter extends BaseAdapter{
      Context c;
      ArrayList&lt;Spacecraft&gt; spacecrafts;
      public CustomAdapter(Context c, ArrayList&lt;Spacecraft&gt; spacecrafts) {
          this.c = c;
          this.spacecrafts = spacecrafts;
      }
      @Override
      public int getCount() {
          return spacecrafts.size();
      }
      @Override
      public Object getItem(int position) {
          return spacecrafts.get(position);
      }
      @Override
      public long getItemId(int position) {
          return position;
      }
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if(convertView==null)
          {
              convertView= LayoutInflater.from(c).inflate(R.layout.list_item,parent,false);
          }
          TextView nameTxt= (TextView) convertView.findViewById(R.id.LevelText);
          //TextView propTxt= (TextView) convertView.findViewById(R.id.propellantTxt);
          //TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);
          ImageView iv=(ImageView) convertView.findViewById(R.id.imageViewStars);
  
          final Spacecraft s= (Spacecraft) this.getItem(position);
          nameTxt.setText("Level " + s.getLevel());
          iv.setImageResource(R.drawable.lock);
  
          return convertView;
      }
  }
  </code></pre>
  
  <p>The first class that loads that references the listview...This is a snipet.</p>
  
  <pre><code>public class GoogleAuthentication extends AppCompatActivity implements Serializable,
          GoogleApiClient.OnConnectionFailedListener {
  
      private static final String TAG = "SignInActivity";
      private static final int RC_SIGN_IN = 9001;
      public final static String USER_ID = "USER_ID";
  
      private GoogleApiClient mGoogleApiClient;
      private FireBase mFirebase;
  
      DatabaseReference db;
      FirebaseHelper helper;
      CustomAdapter adapter;
  
      private TextView mStatusTextView;
      private ProgressDialog mProgressDialog;
      MenuInflater inflater;
      private boolean  mSignedIn;
      private GoogleSignInAccount acct;
      private NumberPicker picker;
      ArrayList Levels;
      String[] LevelsArray;
  
  
      ListView lv;
  
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_google_authentication);
  
          //INITIALIZE FIREBASE DB
          db = FirebaseDatabase.getInstance().getReference();
          helper = new FirebaseHelper(db);
          //ADAPTER
          lv = (ListView) findViewById(R.id.LevelListView);
  // Configure sign-in to request the user's ID, email address, and basic
  // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
          GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                  .requestEmail()
                  .build();
  
          // Build a GoogleApiClient with access to the Google Sign-In API and the
  // options specified by gso.
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                  .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                  .build();
      }
  private void updateUI(boolean signedIn)
      {
          mSignedIn = signedIn;
          if (signedIn)
          {
              Spacecraft s = new Spacecraft();
              s.setLevel(0);
  
  
              adapter = new CustomAdapter(this, helper.retrieve());
              lv.setAdapter(adapter);
  
              invalidateOptionsMenu();
          } else {
              //mStatusTextView.setText(R.string.signed_out);
          }
      }
  }
  </code></pre>
  
  <p>So when the GoogleAuthentication class loads, it initializes and gets a reference to the Firebase database. Then it sign's the user into their Google Account. Then the UpdateUI method runs. This method sets the listview adapter and calls the FireBase Helper Class and runs the retrieve method. The retrieve method sets the ChildEventListener.</p>
  
  <p>But, when all of this is happening, I want to query the Firebase database and display 2 listview rows. The first one should say Level 2 and the next one should say level 3. Both rows should display the lock drawable according to the way I have the GetView method written right now. But, when I run this right now, I just see a blank screen. I can see that I am retrieving data and populating the ArrayList, but the data is not making it into the ListView for some reason. Any help would be appreciated.Thanks.</p>