title: Unable to retrieve data from Firebase - Android Studio (Java)
body: <p>I am trying to read a value from a database but does not seem to be working. The purpose of the code is to take in a counter value from a user's UID in Firebase, that will change the source of an image. The counter is changed in another activity and works fine. I feel I am close but not sure what do do next. I have tried to print out the value but it does not print in the console.</p>
  <p>Full disclaimer, I am new to android development and firebase so I apologise in advance if it is a simple mistake/correction.</p>
  <p>Activity calling the database UPDATED:</p>
  <pre><code>public class Menu extends AppCompatActivity {
      private static final String TAG = &quot;Menu&quot;;
      Button btn;
      public ImageView image;
      public static int counter;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_menu);
  
          // Add an image
          image = findViewById(R.id.imageView2);
          image.setImageResource(R.drawable.g1);
          //image.setImageResource(getIntent().getIntExtra(&quot;myImageResource&quot;,R.drawable.g1));
         
          final FirebaseDatabase database = FirebaseDatabase.getInstance();
          DatabaseReference ref = database.getReference(&quot;counter&quot;);
  
          
          ref.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  menuCounter uCount = dataSnapshot.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).getValue(menuCounter.class);
                  System.out.println(&quot;Simon says&quot; + uCount.counter);
                  counter = uCount.counter;
  
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  System.out.println(&quot;The read failed: &quot; + databaseError.getCode());
              }
          });
  
          System.out.println(&quot;Counter is: &quot; + counter);
  
          if (counter == 1) {
              image.setImageResource(R.drawable.g2);
          }
  
          btn = findViewById(R.id.button1);
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent WIP = new Intent(Menu.this, WIP.class);
                  startActivity(WIP);
              }
          });
      }
  }
  </code></pre>
  <p>Getter class:</p>
  <pre><code>package com.alanlyne.tbm;
  
  public class menuCounter {
      int counter;
  
      public menuCounter() {
      }
  
      public menuCounter(int counter) {
          this.counter = counter;
      }
  
      public int getCounter() {
          return counter;
      }
  }
  </code></pre>
  <p>Activity changing the counter value:</p>
  <pre><code>public class WIP extends AppCompatActivity {
      Button btn;
  
      DatabaseReference databaseName;
  
      FirebaseAuth mFirebaseAuth;
      private FirebaseAuth.AuthStateListener mAuthStateListener;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_wip);
  
          databaseName = FirebaseDatabase.getInstance().getReference(&quot;counter&quot;);
  
          btn = findViewById(R.id.button1);
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Menu.counter = 1;
                  Intent act2= new Intent(WIP.this,Menu.class);
  //                act2.putExtra(&quot;myImageResource&quot;, R.drawable.g2);
                  startActivity(act2);
  
                  addCounter();
  
              }
          });
      }
  
      private void addCounter() {
          Menu.counter = 1;
  
          String id = databaseName.push().getKey();
  
          menuCounter counter = new menuCounter(Menu.counter);
  
          databaseName.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(counter);
          }
      }
  </code></pre>
  <p>Firebase image:</p>
  <p><a href="https://i.stack.imgur.com/xeRY3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xeRY3.png" alt="The XMxqL line is that user's own UID that was generated when they signed up" /></a></p>