0

I have the following database node:

  "ButtonState" : {
    "payEnabled" : 0,
    "requestEnabled" : 1
  },

When a driver accepts a ride request from the rider,the value of "payEnabled = 1". I am trying to get the value of payEnabled after the driver accepts the request, but it errors out and I get this:

 java.lang.NumberFormatException: For input string: "{payEnabled=0, requestEnabled=1}"

The code I am using to get the data is:

 rootRef = rootRef.child("ButtonState");

    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                int payEnabled = Integer.valueOf(dataSnapshot.getValue().toString());
                Log.e(TAG, "payEnabled = " + payEnabled);
                if (payEnabled == 1) {
                    pulseAnimation();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

What am I doing wrong, and how can I just get the value of "payEnabled"?

halfer
  • 19,824
  • 17
  • 99
  • 186
LizG
  • 2,246
  • 1
  • 23
  • 38

1 Answers1

2

You're listening to ButtonState and interpreting that as a number. But ButtonState is a JSON object, while payEnabled is a property inside ButtonState. Given that you're only interested in payEnabled, I'd attach the listener to only that property:

rootRef.child("payEnabled").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            int payEnabled = dataSnapshot.getValue(Integer.class);
            Log.e(TAG, "payEnabled = " + payEnabled);
            if (payEnabled == 1) {
                pulseAnimation();
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807