0
a2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //below
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
        ref.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                ra2 = dataSnapshot.child("a2").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //fetching face value
        ref.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                rface = dataSnapshot.child("face").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //fetching nothingdb which is equals to "zero" in Firebase by default.

        ref.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                nothing = dataSnapshot.child("nothingdb").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //fetching addeddb which is equals to "one" in Firebase by default.

        ref.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                added = dataSnapshot.child("addeddb").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        //starting if condition
        if (ra2==nothing){

            if (rface==nothing){
                a2.setBackgroundResource(R.drawable.rounded);{
                    Firebase refChild = ref2.child("a2");
                    refChild.setValue("rounda2");
                    refChild = ref2.child("face");
                    refChild.setValue("one");
                }

                else if (rface==added) {
                    a2.setBackgroundResource(R.drawable.crossed);
                    {
                        Firebase refChild = ref2.child("a2");
                        refChild.setValue("crossa2");
                        refChild = ref2.child("face");
                        refChild.setValue("zero");
                    }
                }
            }
        });
    }
});

I am trying to use the above written code so that once I click on the button the background image of the button is changed based on the data present in the Firebase but the If Condition is not working for reason.

It just ignores if (ra2==nothing){ and also the next if conditions.

koceeng
  • 2,169
  • 3
  • 16
  • 37
Sagar Duhan
  • 257
  • 3
  • 15
  • 1
    What is data type of ra2 and nothing? – Kaushal28 Mar 01 '17 at 02:52
  • Compare using equals method – Kaushal28 Mar 01 '17 at 02:52
  • 2
    A more significant problem is that the `onDataChange()` callbacks are asynchronous. `ra2`, `nothing`, `added`, and `rface` will not contain valid results when you compare them for equality because the `onDataChange()` methods will not yet have executed. See this answer for more explanation: http://stackoverflow.com/a/41409942/4815718 – Bob Snyder Mar 01 '17 at 03:26
  • Check this question, http://stackoverflow.com/questions/40260334/how-to-detect-if-valueeventlistener-has-fetched-data-or-not-in-firebase – Darshan Soni Mar 01 '17 at 04:21
  • can you share structure of the database ? – Ashutosh Barthwal Mar 01 '17 at 09:31

2 Answers2

1

The listener onDataChange() callbacks are asynchronous. ra2, nothing, added, and rface will not contain valid results when you compare them for equality because the onDataChange() methods will not yet have executed. This answer to a related question explains the execution order in more detail.

In addition, to compare Strings for equality you cannot use the == operator. You must use the equals() method or TextUtils.equals().

Community
  • 1
  • 1
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • Thank you very much @qbix, I did not knew about the asynchronous callbacks. It worked after reframing my problem. And thanks the advice on == operator. – Sagar Duhan Mar 01 '17 at 12:21
1

@qbix's answer is true, your if condition actually work but it does not receive the value of ra2, rface, nothing, and added yet.

Also, why should you create 4 different ValueEventListener while you listening to the same reference? You can use just 1 listener then put your if condition inside onDataChange, like this:

a2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ra2 = dataSnapshot.child("a2").getValue(String.class);
                rface = dataSnapshot.child("face").getValue(String.class);
                nothing = dataSnapshot.child("nothingdb").getValue(String.class);
                added = dataSnapshot.child("addeddb").getValue(String.class);

                //starting if condition
                if (ra2.equals(nothing)) {
                    if (rface.equals(nothing)) {
                        a2.setBackgroundResource(R.drawable.rounded);
                        Firebase refChild = ref2.child("a2");
                        refChild.setValue("rounda2");
                        refChild = ref2.child("face");
                        refChild.setValue("one");
                    } else if (rface.equals(added)) {
                        a2.setBackgroundResource(R.drawable.crossed);
                        Firebase refChild = ref2.child("a2");
                        refChild.setValue("crossa2");
                        refChild = ref2.child("face");
                        refChild.setValue("zero");
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, "onCancelled", databaseError.toException());
            }
        });       
    }
});
Janice Kartika
  • 502
  • 1
  • 3
  • 14