Android Studio dataSnapshot exists but nothing is getting printed Firebase - android

I'm confused as to why this is not working and I have tried debugging it a lot and I have no idea why is this not working
FirebaseDatabase.getInstance().getReference().child("users").child("name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.d("dataSnapshot", "exists");
for (DataSnapshot snapshot1 : dataSnapshot.getChildren()) {
Log.d("inside snapshot", "snapshot not null");
String name = snapshot1.getValue().toString();
Log.d("user created", "snapshot not null");
try {
Log.d("name", name);
Log.d("decrypt", aes.Decrypt(name));
} catch (UnsupportedEncodingException e) {
Toast.makeText(EnterName.this, "Exception", Toast.LENGTH_LONG);
e.printStackTrace();
}
Toast.makeText(EnterName.this, "logged", Toast.LENGTH_LONG);
}
}
else
{
Toast.makeText(EnterName.this,"doesnt exist", Toast.LENGTH_SHORT);
}
}
I'm getting the log that dataSnapshot exists but nothing after that. Firebase data is
Anyone has any idea? Thank you.

You're listening to just the users/name property, which means you get a snapshot with the key name and a string value "check". A string value doesn't have any children, so dataSnapshot.getChildren() returns an empty list.
To print the value of your snapshot:
FirebaseDatabase.getInstance().getReference().child("users").child("name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.d("dataSnapshot", dataSnapshot.getValue(String.class));
}
...

Related

How to get Unique Key of childs in firebase

i am new to Firebase
so i have a scenario where i have to set a check that if a hotel is already present in FavouriteHotels Node, then this must Toast that this hotel is already present in FavouriteHotels Node else Just Store that clicked hotel. like in this code.
final String favHotel_Id = myRef.push().getKey();
myRef.child("Users").child(mUser.getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot userSnapshot : snapshot.getChildren()) {
if (!userSnapshot.child("FavouriteHotels").exists()){
Log.d(TAG, "onDataChange: "+userSnapshot.getChildren());
if (Objects.equals(hotelArrayList.get(position).getHotelName(), userSnapshot.child("Name").getValue() )) {
Toast.makeText(mContext, "Already saved to favourite hotels", Toast.LENGTH_SHORT).show();
} else {
if (favHotel_Id != null) {
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Name").setValue(hotelArrayList.get(position).getHotelName());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Image").setValue(hotelArrayList.get(position).getImageID());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("rating").setValue(hotelArrayList.get(position).getHotelRating());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Price").setValue(hotelArrayList.get(position).getHotelPrice());
}
}
}else {
Toast.makeText(mContext, "No Data Found", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
one thing i want to mention is, i am saving this hotel item with its properties from ArrayList where i am getting that
if (Objects.equals(hotelArrayList.get(position).getHotelName(), userSnapshot.child("Name").getValue() )) {
Toast.makeText(mContext, "Already saved to favourite hotels", Toast.LENGTH_SHORT).show();
} else {
if (favHotel_Id != null) {
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Name").setValue(hotelArrayList.get(position).getHotelName());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Image").setValue(hotelArrayList.get(position).getImageID());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("rating").setValue(hotelArrayList.get(position).getHotelRating());
myRef.child("Users").child(mUser.getUid()).child("FavouriteHotels").child(favHotel_Id).child("Price").setValue(hotelArrayList.get(position).getHotelPrice());
}
}
i want to fetch the unique Key of hotel highlighted in circle and putting a check on the Name of hotel that if hotelArrayList.get(position).getHotelName(), userSnapshot.child("Name").getValue() then show me a toast that this hotel is already present in the db.
you can get the push key like this.
Query query = FirebaseDatabase.getInstance().getReference("your node path");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
String key = snapshot.getKey();
Log.i(TAG,key);
}
}

how to fix dataSnapshot.getValue() retrieves null?

Okay so I am trying to pull from my database the email of a single user under the userID, and it retrieves null.
The userUID is authUID.
My Database structure
When I change the line to :
String value = dataSnapshot.child("email").getKey();
it retrieves the right key so I am standing on the right position of the database as far as I understand.
myDatabaseRef.child(userUID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
String value = dataSnapshot.child("email").getValue(String.class);
//String value = dataSnapshot.child("email").getKey(); gets email
Log.d("TAG", "onDataChange: " + value);
}
catch (Exception ex){
Log.d("PROBLEM",ex.toString());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The error that I get:
2019-07-30 10:42:29.566 14673-14673/com.example.h210.housecommitteemanage D/TAG: onDataChange: null
Try this, below code will fetch emails of all the users :
FirebaseDatabase.getInstance().getReference("DataUsers").child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
Iterable<DataSnapshot> snapshotIterable = dataSnapshot.getChildren();
for (DataSnapshot aSnapshotIterable : snapshotIterable) {
String value = aSnapshotIterable.child("email").getValue(String.class);
Log.e(Constants.TAG, "Users email : " + value);
}
}catch (Exception ex){
Log.e("PROBLEM",ex.toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

firebase: get value with a given key

mheadingReference1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot : dataSnapshot.getChildren()) {
Log.e("one", "======="+snapShot.child("id").getValue(String.class));
Log.e("one", "======="+snapShot.child("fullname").getValue(String.class));
Log.e("one", "======="+snapShot.child("password").getValue(String.class));
Log.e("one", "======="+snapShot.child("type").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I would like to get the values from firebase and I know the keys of the object. Like "key: id" "value: 123456". The code above displays NULL. How will I be able to get the values, like output "123456" alone. Thanks in advance

I'm not able to get data from a Firebase database

I'm using Firebase for an Android project and it works well while pushing the data to a server, but when retrieving the data, I get nothing.
here is the structure for my firebase
and here is my code for retrieving the data form Firebase
mRef.child("User").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
counter = dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
post = postSnapshot.getValue(SettersGetters.class);
nicuNames.add(post.getNicuName());
nicuAddresses.add(post.getNicuAddress());
niucCount.add(post.getNicuCount());
nicuPrices.add(post.getNicuCount());
nicuPhones.add(post.getNicuPhone());
nicuUpdate.add(post.getNicuUpdate());
nicuLat.add(post.getNicuLat());
nicuLng.add(post.getNicuLng());
nicuDes.add(post.getNicuDesc());
a++;
Toast.makeText(MapsActivity.this,"jhkjhk", Toast.LENGTH_LONG).show();
System.out.println(nicuNames.get(0));
}
//Toast.makeText(MapsActivity.this,"jhkjhk" + a, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
While making a Toast to print at least the number of Children, it returns 0 every time
Toast.makeText(MapsActivity.this,a+"",Toast.LENGTH_SHORT).show();
You want to listen to children events of User key, not value events.
Replace
mRef.child("User").addValueEventListener(new ValueEventListener() {
With
mRef.child("User").addChildEventListener(new ChildEventListener() {
Then, implement
onChildAdded(DataSnapshot snapshot, String previousChildName)
onChildChanged(DataSnapshot snapshot, String previousChildName)
And from there, you map a DataSnapshot to a (preferrably better named class than) SettersGetters.class
try this
mRef.child("User").child("Kfx7408plvgugiMR1Se").addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
counter = dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
post = postSnapshot.getValue(SettersGetters.class);
nicuNames.add(post.getNicuName());
nicuAddresses.add(post.getNicuAddress());
niucCount.add(post.getNicuCount());
nicuPrices.add(post.getNicuCount());
nicuPhones.add(post.getNicuPhone());
nicuUpdate.add(post.getNicuUpdate());
nicuLat.add(post.getNicuLat());
nicuLng.add(post.getNicuLng());
nicuDes.add(post.getNicuDesc());
a++;
Toast.makeText(MapsActivity.this,"jhkjhk", Toast.LENGTH_LONG).show();
System.out.println(nicuNames.get(0));
}
//Toast.makeText(MapsActivity.this,"jhkjhk" + a, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How to check if firebase query is empty

I want to delete the data from Firebase before unauthorizing. The problem is that mFirebaseRef.unauth() works only if query is not empty. But I need it to work even if query is empty.
final Firebase pushNotificationRef = new Firebase(Constant.FIREBASE_URL_PUSHNOTIFICATIONS);
final Query queryRef = pushNotificationRef.orderByChild("deviceToken").equalTo(token);
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()) {
Log.e("MyTag", dataSnapshot.getKey());
pushNotificationRef.child(dataSnapshot.getKey()).removeValue();
}
mFirebaseRef.unauth();
}
use this...
if (dataSnapshot.exists())
// user found
else
// user not found
demo example
Query query = dbstud.child("users").orderByChild("name").equalTo("XYZ");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Toast.makeText(getApplicationContext(), "id = " + snapshot.getKey(), Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getApplicationContext(), "User Not found ", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});

Categories

Resources