TerribleCoder TerribleCoder - 4 months ago 241
Android Question

Android firebase getValue() not working

I have the following JSON file

{
"TestNode" : {
"TestKey" : "TestValue",
"TestKeyTwo" : "TestValueTwo"
},
"TestNode2" : {
"One" : {
"ItemOne" : "email@gmail.com",
"ItemTwo" : "zYMnPqefn7BlGDidpOocff6Xcg==\nBKb1sfaDdgfe0=\n1yVcV0mciIQXtAf/4LqGdDw==\n",
"ItemThree" : "name"
},
"Two" : {
"email" : "emailtwo@gmail.com",
"password" : "xFBvDPcnp/efebUhtefZ0+XMFfw==\ngAb5fSbzAefeQXc=\ng/WdCpusQl2umtPVOefefzjs1A==\n",
"username" : "othername"
}
}
}


The listener code

TestNode.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if(dataSnapshot.hasChild(TestKey)) {
String TestKeyString = TestNode.child(TestKey).getKey();
String TestValueString = TestNode.child(TestKey).getValue();

}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
});


and I get the error


Cant resolve method getValue();


P.S I haven't run the code yet, because of the above error

Answer

Try this code:

 TestNode.addListenerForSingleValueEvent(new ValueEventListener() {

 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    Log.d("TEST","key: "+dataSnapshot.getKey());
    Log.d("TEST","value: "+dataSnapshot.getValue());
 }

 @Override
 public void onCancelled(DatabaseError databaseError) {}
 });

Update
Try this code to get only one child

 TestNode.orderByKey().equalTo(TestKey).addListenerForSingleValueEvent(new ValueEventListener() {

 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    Log.d("TEST","key: "+dataSnapshot.getKey());
    Log.d("TEST","value: "+dataSnapshot.getValue());
 }

 @Override
 public void onCancelled(DatabaseError databaseError) {}
 });
Comments