This is my database in firebase.
This is my java code for traversing database.
mref = new Firebase("https://social-axxxxxcf.firebaseio.com/user");
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
String email=userSnapshot.child("email").getValue().toString();
String urL=userSnapshot.child("url").getKey().toString();
Log.e("Datas= ",email+" " +url);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("TAG= ",firebaseError.getMessage());
}
});
My problem is when i run this code my app getting crash and even error in log is not showing.
Can anyone help me please.
Since you're using ChildEventListener on /user, your onChildAdded will get called for every node under/user. This means you don't need t loop over dataSnapshot.getChildren():
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String email=dataSnapshot.child("email").getValue(String.classs);
String urL=dataSnapshot.child("url").getValue(String.class);
Log.e("email="+email+" url="+url);
}
Check the following link it's the working example to implement firebase with android.
link
Related
when open the application i want to get notification if new data added to firebase realtime database .
but whan i open the app i got notifcation every time i open the it.
what should i do
this is my code.
cam_firebase.child("Detection").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NotNull DataSnapshot dataSnapshot, String s) {
double longt = dataSnapshot.child("longt").getValue(Double.class);
double lat = dataSnapshot.child("lat").getValue(Double.class);
notify_me();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
}
});
I have this "chat-system" that I made with Firebase Database.
To retrieve the messages in the Room I do a Query with an orderByChild(timestamp) then reverse the order so I can get the "newest" Last (Because I start reading from the Bottom).
messageList = new ArrayList<>();
final Query query = chatRef_1.orderByChild("
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
messageList.add(dataSnapshot.getValue(ChatMessage.class));
updateRecyclerView();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
messageList.remove(dataSnapshot.getValue(ChatMessage.class));
updateRecyclerView();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But then I get this result
This only happens with ChildEventListener
The TimeStamp is done by the server
Does anyone have an Idea of what this might be?
UPDATE
Here's the database Structure
Box »» Object (Message)
Green »» MessageID
Blue »» RoomID
How should I retrieve random key from Firebase realtime database that are stored in list?
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("inspirational");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String randomkey=dataSnapshot.getkey();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Use ChildEventListener since this is a list, first get the reference of the database. .child("inspirational") it means we are querying on that location.
Then inside onChildAdded(DataSnapshot dataSnapshot, String s) retrieve your data.
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseRef = mFirebaseInstance.getReferenceFromUrl("https://c7d86.firebaseio.com");
mFirebaseRef.child("bloodgroup").equalTo("B+").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getUpdate(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getUpdate(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
So please help me because I am a new learner for firebase and have to know how to write a query in this.
Firebase Database queries evaluate the children under the node where you execute the query. So in your case when you use mFirebaseRef.child("bloodgroup") it will look for a /bloodgroup and then query the children under that. Since there is no /bloodgroup, there are no results.
The correct code:
mFirebaseRef = mFirebaseInstance.getReference();
DatabaseReference queryLocation = mFirebaseRef.child("the key under bloodfinder-c7d86 in the screenshot");
Query query = queryLocation.orderByChild("bloodgroup").equalTo("B+");
query.addChildEventListener(new ChildEventListener() {
#Override
Note that I had to do some guesses, because you shared a picture of the JSON. For future questions, share the JSON as text. You can get this by clicking the "Export JSON" link in your Firebase Database console.
public class DataService {
private static DataService ourInstance = new DataService();
private DatabaseReference mDatabase;
public static DataService getInstance() {
return ourInstance;
}
public ArrayList<UserDatabase> getFriendList() {
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
final ArrayList<UserDatabase> list = new ArrayList<>();
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserDatabase userDatabase = dataSnapshot.getValue(UserDatabase.class);
list.add(userDatabase);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
return list;
}
}
I am trying to fetch my users database from the users node. It's acting really weird. When I set a breakpoint at the onChildAdded function, it will retrieve all the users and add it to the list.
However, if I don't set a breakpoint there, it is not getting anything and the list size is 0. Does anyone have any idea what is going on? Thanks in advance!
Data is loaded and synchronized from Firebase asynchronously.
It's easiest to see what's causing your problem if you add a few well-placed logging statements:
public ArrayList<UserDatabase> getFriendList() {
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
final ArrayList<UserDatabase> list = new ArrayList<>();
System.out.println("Attaching listener");
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println("Got data");
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
public void onChildRemoved(DataSnapshot dataSnapshot) {}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
public void onCancelled(DatabaseError databaseError) {}
});
System.out.println("Returning list");
return list;
}
The output will print in this order:
Attaching listener
Returning list
Got data
That is probably not the order you expected. But it does explain why the list of empty if you run the code as is.
The reason for this is that Firebase loads and synchronizes the data asynchronously. Like most modern web APIs it doesn't wait for the result to return (that would lead to a horrible user-experience), but instead lets the program continue and calls back into your code when it gets (new) data.
The way to deal with this behavior is to reframe the flow of your program from "first get the list of friends, then do xyz with it" to "whenever the list of friends changes, do xyz with it".
For example say that you simply want to print how many friends there are. Your current approach is probably:
ArrayList<UserDatabase> friends = getFriendList();
System.out.println("There are "+friends.size()+" friends");
With Firebase one way to handle this is to move the operation into getFriendList():
public void getFriendList() {
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
final ArrayList<UserDatabase> list = new ArrayList<>();
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserDatabase userDatabase = dataSnapshot.getValue(UserDatabase.class);
list.add(userDatabase);
System.out.println("There are "+friends.size()+" friends");
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
public void onChildRemoved(DataSnapshot dataSnapshot) {}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
public void onCancelled(DatabaseError databaseError) {}
});
}