Burak İlçim
Burak İlçim

Reputation: 23

Firebase OnDataChange function triggered wrong time?

I have a basic program for testing Firebase. I have 2 buttons: first is writing to Firebase database "a", second is writing "b" and I am checking them with a service:

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = dataSnapshot.getValue(String.class);
            if(value=="a")
            {
                ToneGenerator toneGenerator= new   ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);
                //this will play tone for 2 seconds.
                toneGenerator.startTone(ToneGenerator.TONE_DTMF_1, 500);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                toneGenerator.startTone(ToneGenerator.TONE_DTMF_1, 500);

            }

            if(value=="b")
            {
                ToneGenerator toneGeneratora= new   ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);
                //this will play tone for 2 seconds.
                toneGeneratora.startTone(ToneGenerator.TONE_DTMF_3, 500);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                toneGeneratora.startTone(ToneGenerator.TONE_DTMF_3, 500);

            }

The problem is, even I have no internet connection, I mean even I can't write to Firebase database a or b when I click the buttons, OnDataChanged function is still triggered. I want to do something just when the database data is changed.

UPDATE : When I changed the Database manually OnDataChange still not triggered.Its working just once while program is starting.

Upvotes: 1

Views: 472

Answers (1)

Tata Fombar
Tata Fombar

Reputation: 349

Use a ChildEventListener then you can put the code you want to execute inside the onChildChanged() method.

Upvotes: 2

Related Questions