Manali Bhavsar
Manali Bhavsar

Reputation: 21

How I can increment counter in firebase database forever through android application when the user clicks on the button

Here is my code.

package com.example.manali.likeex;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;



public class MainActivity2 extends Activity implements View.OnClickListener {
    Button senlike;
    private DatabaseReference mdatabase;
    private DatabaseReference user1;

     int c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button sendlike = (Button) findViewById(R.id.b1);
        mdatabase= FirebaseDatabase.getInstance().getReference();
        user1=mdatabase.child("Teacher");
        sendlike.setOnClickListener(this);

    }

//when user clicks on button counter on firebase get increased public void onClick(View view) {

        if(user1!=null)
        {
            c++;

        }



//sets value to user1 node
        user1.setValue(c);

    }
    public DatabaseReference getUser1()
    {
        return user1;

    }
}

This code only increases counter when app is active. When we close the app, and restart it again. The counter starts again with 1. Hence I want to maintain this counter.

Upvotes: 2

Views: 5526

Answers (2)

Sunny Sultan
Sunny Sultan

Reputation: 1180

DatabaseReference upvotesRef = ref.child("server/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes");
upvotesRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        Integer currentValue = mutableData.getValue(Integer.class);
        if (currentValue == null) {
            mutableData.setValue(1);
        } else {
            mutableData.setValue(currentValue + 1);
        }

        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
       System.out.println("Transaction completed");
    }
});

Upvotes: 6

Kurt Acosta
Kurt Acosta

Reputation: 2557

You could have a count column under your Teacher table:

App
---Teacher
------Count

Then with that you could query the count and put it in a variable so you could increment the value then update the value in firebase:

public void onClick(View view) {
    DatabaseReference ref = mdatabase.child("Teacher");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int count = (int) dataSnapshot.child("count").getValue();
            ref.child("count").setValue(count++);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) { }
    });   
}

Upvotes: 3

Related Questions