2

At this line of my code in my OnDataChange() method in the ValueEvenListener:

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

I'm getting a DatabaseException with the error Failed to convert a value of type java.util.HashMap to int.

However, in my database, you can take a look at the image below:

enter image description here

It is obviously not a HashMap but an int. Is this a bug or am I doing something wrong? What can I do to fix it? Why is it retrieving a Hashmap when the value is int?

Full dataSnapshot:

final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
        database.child("Campaigns").child(key).child("count");
        database.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int latest = dataSnapshot.getValue(Integer.class);
                button.setText(latest + "");
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(context, context.getString(R.string.error) + ": " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

Database:

Campaigns:{
  -JDKKDJIIDJFIDJKDK:{
      count:2432
   }
}
10
  • share the declaration of dataSnapshot and also what you are adding to it Commented Aug 9, 2016 at 8:21
  • @AndroidMechanic sure, ust a sec Commented Aug 9, 2016 at 8:23
  • if the dataSnapshot has only the value that u want..in our case 2580 try int latest = Integer.parseInt(dataSnapshot); also, maybe you should convert datasnapshot.toString() but i dont remember EDIT: if this doesnt work please Log the dataSnapshot so i can provide you with full answer Commented Aug 9, 2016 at 8:54
  • It didn't work, I get a NumberFormatException. And please explain what do you mean by "Log the dataSnapshot". Thank you for trying to help Commented Aug 9, 2016 at 9:07
  • inside the OnDataChange insert this line - > Log.w("eeee", dataSnapshot.toString()); , then look in the logcat after you run the app, and search for something with the name "eeee" Commented Aug 9, 2016 at 9:10

4 Answers 4

7

It turns out I had to child dataSnapshot to my destination again. E.g:

int latest = dataSnapshot.child("Campaigns").child(key).child("count").getValue(Integer.class);

By default dataSnapshot is actually my whole database.

Sign up to request clarification or add additional context in comments.

Comments

4

Your ValueEventListener is attached to the whole database.

// This line gets a reference to the whole database
final DatabaseReference database =  FirebaseDatabase.getInstance().getReference();

// This line creates a child DatabaseReference, but you don't assign
// the child to a variable
database.child("Campaigns").child(key).child("count");

// This line adds a ValueEventListener to the whole database
database.addValueEventListener(new ValueEventListener() {

What you want instead is this:

final DatabaseReference database = FirebaseDatabase.getInstance().getReference()
final DatabaseReference countRef = database.child("Campaigns").child(key).child("count")
countRef.addValueEventListener(new ValueEventListener() {
    // ...
});

You can see that in the latter example the ValueEventListener is attached to the child reference, not to the root.

Comments

3

This should work if we assume that the dataSnapshot is right.

final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database = database.child("Campaigns").child(key).child("count"); // replaced

        database.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int latest = Integer.valueOf(dataSnapshot.getValue().toString()); // replaced
                button.setText(latest + "");
            }

            @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(context, context.getString(R.string.error) + ": " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

Reminder : addValueEventListener will run everytime there is a change in the dataSnapshot. If you want to run it just once use addListenerForSingleValueEvent instead.

6 Comments

I'll try it. And addValueEventListener is what I want,, thanks
if this doesn't work that means is something wrong with the dataSnapshot and that you should show it even if its 20 line of codes. Or edit your question and insert an example of how your data structure is.
Now it gives me a NumberFormatException since dataSnapshot.getValue().toString() is returning the same as dataSnapshot.toString() , which is a HUGE string (20+ lines)
edit the line "database.child("Campaigns").child(key).child("count");" to "database = database.child("Campaigns").child(key).child("count");"
okay glad you came up with a solution, you could post it as an aswer for the other users
|
0
HashMap<String, String> hashMap = (HashMap<String, String>) dataSnapshot1.getValue();
                System.out.println("hehe " + hashMap);
                String demo = String.valueOf(hashMap.get("sets"));
                String name = String.valueOf(hashMap.get("name"));

The COde Above ^^^ Worked fine for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.