title: Auto-increment a value in Firebase
body: <p>How do I auto increment a value stored in Firebase from an Android client?</p>
  
  <p>Currently: I declare <code>int id = 1</code>. When I increment, I see the values 2, 3 etc. being stored. That's fine, but when I re-run the project, <code>id</code> is set equal to 1 again.</p>
  
  <p>I want it to behave like a static variable, so I can create an id which will go from 1 to infinity without resetting.</p>
  
  <p><strong>UPDATED FAILED</strong></p>
  
  <p>I used the following to pass the Firebase reference and a string to the function incrementCounter. </p>
  
  <pre><code>if(language_chosen.equalsIgnoreCase("english"))
   {
     Firebase publRef = f.child("Language").child("English").child("Message");
     Firebase newPublRef = publRef.push();
     writeMsgActivity demo = new writeMsgActivity();
     demo.incrementCounter(newPublRef,the_msg_enter);                                 
    }
  </code></pre>
  
  <p>Now I try to use the passed reference and string at   <code>public void incrementCounter(Firebase publref,String my_msg)</code> in the oncomplete method but it gives me an error.</p>
  
  <pre><code>   public void incrementCounter(Firebase publref,String my_msg) {
      publref.runTransaction(new Transaction.Handler() {
          @Override
          public Transaction.Result doTransaction(final MutableData currentData) {
              if (currentData.getValue() == null) {
                  currentData.setValue(1);
              } else {
                  currentData.setValue((Long) currentData.getValue() + 1);
              }
  
              return Transaction.success(currentData);
          }
  
          public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
              if (firebaseError != null) {
                  System.out.println("Firebase counter increment failed.");
              } else {
                  System.out.println("Firebase counter increment succeeded.");
  
                  Map&lt;String, Object&gt; publ = new HashMap&lt;String, Object&gt;();
                  publ.put("pubMsg", my_msg);
                  publ.put("id",currentData);
                  publref.setValue(publ);
              }
          }
      });
  }
  </code></pre>
  
  <p><strong>UPDATED SOLVED</strong></p>
  
  <pre><code>                final Firebase upvoteref = new Firebase("https://shareurday.firebaseio.com/Message/"+msg_id+"/upvotes"); 
  
              upvoteref.runTransaction(new Transaction.Handler() {
                  @Override
                  public Transaction.Result doTransaction(final MutableData currentData) {
                      if (currentData.getValue() == null) {
                          currentData.setValue(1);
                      } else {
                          currentData.setValue((Long) currentData.getValue() + 1);
                      }
                      return Transaction.success(currentData);
                  }
  
                  public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
                      if (firebaseError != null) {
                          System.out.println("Firebase counter increment failed.");
                      } else {
                          System.out.println("Firebase counter increment succeeded.");
                      }
                  }
              });
  </code></pre>
  
  <p>The variable msg_id is the random generated id from push.</p>