title: Retrieve Real time database Counter Value with transactions
body: <p>I'm setting up a counter for a button "submit" to count products quantity in firebase database,it should increment the quantity +1</p>
  
  <p>I have tried transactions for that and  it works.</p>
  
  <p>I need to retrieve the last value stored in the database.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      Button submit, fetch;
      DatabaseReference rootRef, demoRef;
      TextView demoValue;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          demoValue =  findViewById(R.id.tvValue);
          submit =  findViewById(R.id.btnSubmit);
          fetch =  findViewById(R.id.btnFetch);
  
          //database reference pointing to root of database
          rootRef = FirebaseDatabase.getInstance().getReference().child("demo").child("Products");
          demoRef=rootRef.child("quantity");
  
  
  
  
  
          //database reference pointing to demo node
  
          submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
  
  demoRef.runTransaction(new Transaction.Handler() {
      @Override
      public Transaction.Result doTransaction(MutableData mutableData) {
          Long value = mutableData.getValue(Long.class);
          if (value == null) {
              mutableData.setValue(0);
          }
          else {
              mutableData.setValue(value - 1);
          }
  
          return Transaction.success(mutableData);
      }
  
      @Override
      public void onComplete(DatabaseError databaseError, boolean b,
                             DataSnapshot dataSnapshot) {
      }
  });
  
              }
          });
  
          fetch.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  rootRef.child("quantity").addListenerForSingleValueEvent(new ValueEventListener() {
                      @Override
                      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  
                          Integer value = dataSnapshot.getValue(Integer.class);
  
  
  
                          demoValue.setText(value);
                      }
  
                      @Override
                      public void onCancelled(DatabaseError databaseError) {
                      }
                  });
              }
          });
      }
  }
  </code></pre>
  
  <p>When i press the fetch button to show value stored in the database , the app do nothing or crashes!!</p>
  
  <p>My Logcat</p>
  
  <pre><code> android.content.res.Resources$NotFoundException: String resource ID #0xa2
          at android.content.res.Resources.getText(Resources.java:472)
          at android.widget.TextView.setText(TextView.java:4465)
          at com.example.ahmed.firebaseupdatequatity.MainActivity$2$1.onDataChange(MainActivity.java:95)
          at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.5:183)
          at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
          at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
          at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
          at android.os.Handler.handleCallback(Handler.java:815)
          at android.os.Handler.dispatchMessage(Handler.java:104)
          at android.os.Looper.loop(Looper.java:207)
          at android.app.ActivityThread.main(ActivityThread.java:5728)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
  </code></pre>
  
  <p>Check my database structure here:</p>
  
  <p><a href="https://i.stack.imgur.com/U55X5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U55X5.png" alt="enter image description here"></a></p>