0

Hi I am planing to create an application, and am new to android.

I am using Firebase as backend and i am trying yo update and score every time the users win.

I am using this and the app crashes:

public void updatescore (){
    FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    String uid = currentFirebaseUser.getUid();
    final DatabaseReference mostafa = ref.child(uid);


    mostafa.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int saldo2 = dataSnapshot.getValue(Integer.class);
            saldo saldoide = new saldo(saldo2 + 100);
            mostafa.setValue(saldoide);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

my db is very simple :

KQSxF0e901gCPwgu2npvAm0BsKy2

 saldo2: 0

Code:

 if (ejugador1.equals("paper") && ejugador2.equals("stone")){
        resultado = "You Win";
                        updatescore ()
    }
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • can you share the error trace message? – Rohit Padma Jun 29 '17 at 21:24
  • @RohitPadma com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int – Chris Dopazo Jun 29 '17 at 21:32
  • Try this line :- int saldo2 = Integer.valueof(dataSnapshot.getValue().toString()); – Rohit Padma Jun 29 '17 at 21:40
  • @RohitPadma the previous error dissapear , but now i have this one: java.lang.NumberFormatException: For input string: "{saldo2=0}" – Chris Dopazo Jun 29 '17 at 21:47
  • dataSnapshot.getValue().toString() is returning {saldo2=0} so you cannot convert that to integer. So Modified code so change this line :- final DatabaseReference mostafa = ref.child(uid).child('saldo2'); – Rohit Padma Jun 29 '17 at 21:51
  • @RohitPadma the first time i win is works , but the secound time crashes with this error: java.lang.NumberFormatException: For input string: "{saldo2=100}" (Every time you win 100 points are added) – Chris Dopazo Jun 29 '17 at 21:58

2 Answers2

1

You need to change this line of code:

int saldo2 = dataSnapshot.getValue(Integer.class);

with

int saldo2 = dataSnapshot.child("saldo2").getValue(Integer.class);

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Alex, remember me? :) Can you please take a look at my new question: https://stackoverflow.com/questions/44743530/node-with-more-than-1-child-is-not-getting-removed Nobody's giving an answer even after setting a +50 bounty... –  Jun 29 '17 at 22:44
  • Yes, i remember but unfortunately i don't know Javascript. Sorry. – Alex Mamo Jun 30 '17 at 05:57
0

Try this line of code :-

   int saldo2 = Integer.valueof(dataSnapshot.getValue().toString());
Rohit Padma
  • 603
  • 5
  • 15
  • the previous error dissapear , but now i have this one: java.lang.NumberFormatException: For input string: "{saldo2=0}" – Chris Dopazo Jun 29 '17 at 21:47
  • @ChrisDopazo dataSnapshot.getValue().toString() is returning {saldo2=0} as string so you cannot convert that to integer. So try this line :- final DatabaseReference mostafa = ref.child(uid).child('saldo2'); – Rohit Padma Jun 29 '17 at 21:52
  • the first time i win is works , but the secound time crashes with this error: java.lang.NumberFormatException: For input string: "{saldo2=100}" (Every time you win 100 points are added) – Chris Dopazo Jun 29 '17 at 21:58