I have stored data into the firebase real time database, but unable to retrieve a set of data from the database.. below i have attached a pic which shows the skeleton of my firebase database
Tell me how i will retrieve status data from database..
Sorry i changed my database structure like below..
Database Structure
After clicking the status
Assuming that the screen-shot is coorect and the structure is Firebase-root -> messages -> status, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference statusRef = rootRef.child("status");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String status = ds.getValue(String.class);
Log.d("TAG", status);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
statusRef.addListenerForSingleValueEvent(eventListener);
Related
I already have knowledge in retrieving data from firebase. The thing is, the data i want to retrieve lies under 3 parent nodes, and I can't figure any way how to retrieve data from the database 3 nodes deep. What I want is to get all the values of the key named "name" and display it in a listview.
To display all those names, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference medicinesRef = rootRef.child("Medicines");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
Log.d("TAG", name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
medicinesRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
amoxcillin
paracetamol
mefenamic
In my DB each user has friends. And I need to get 50 last posts of the user's friends. See below structure of posts of each user in Firebase DB.
So what is optimal way to do that? If a user has for example 150 friends, then my Android app should have as many listeners as friends. Is it ok to have 150 or more DB listeners in the app? Also what is the way to get all friends' posts in single query? Thanks.
root
- posts
- {userUid}
- {postUid}
- text
- dateCreated
You can have as many listeners as you want with the condition to remove them according to your life-cycle of your activity. But in your case, you don't need 150 listeners, you only need just one. Assuming that the node under the user is is named friends, you can attach a single listener on friends node and then iterate over the DataSnapshot object using getChildren() method. Assuming that your database looks simmilar with this:
Firebase-root
|
--- users
|
--- uid1
|
--- friends
|
--- friendId1: true
|
--- friendId2: true
To get all the friends that corespond to a particular user, please use the following code:
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child(friends);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendId = ds.getKey();
Log.d("TAG", friendId);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
friendsRef.addListenerForSingleValueEvent(eventListener);
To get all post that of all users, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference postsRef = rootRef.child("posts");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String uid = ds.getKey();
DatabaseReference uidRef = postsRef.child(uid);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String text = dSnapshot.child("text").getValue(String.class);
long dateCreated = dSnapshot.child("dateCreated").getValue(Long.class);
Log.d("TAG", text + " / " + dateCreated)
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(valueEventListener);
What I would do is have a friendsListener, once initialized I would loop through my friends and register their reference in my listener vs 150 listeners.
You can have 150+ listeners but the less the better. Maybe you can just watch all posts instead of listening to every friend separtely.
To get all posts of a friend once try:
ref('posts').child(friendUid).once('value').then(function(snapshot) {
console.log(snapshot.val());
});
To get the last 50 posts of a friend once try:
ref('posts').child(friendUid).orderByChild("dateCreated").limitToLast(50).once('value').then(function(snapshot) {
console.log(snapshot.val());
});
(depending on your dateCreated property you might need to use limitToFirst instead of limitToLast. Check the Firebase Query docu for more information.)
I am trying to retrieve a list of friends for a user so I can display them in a list view. The friends and user info is structured like this in my firebase database:
So basically, I want to take the user ids listed in the friends part and query my users data to get all the info under every node that is in that set of user ids. How can I achieve this using the android firebase database sdk querying? I would like to be able to retrieve all the users in a single database query.
Thanks.
You cannot get that data in single query, you need to query your database twice. This is a common practice when it comes to Firebase. Assuming that friends and users nodes are direct childs of your Firebase root, to achieve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendIdRef = rootRef.child("friends").child(friendId)
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference usersRef = rootRef.child("users").child(key);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String username = dSnapshot.child("username").getValue(String.class);
Log.d("TAG", username);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
friendIdRef.addListenerForSingleValueEvent(valueEventListener);
It will print all user names of those particular users. One more thing to note, is that you don't need to add in your database those friends that have the value of false, only those with the value of true.
I have a firebase database with the next structure:
PPostdb-e1e1d
Datos
USERID
name
mail
USERID
Post
PostTITLE
PostTITLE
text
I'm developing an app for publish simple post, before I added the option to publish a post, I was getting the user's data and showing it in an activity using the nex code:
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
...
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
showPersonalData(dataSnapshot);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void showPersonalData(DataSnapshot dataSnapshot) {
FirebaseUser user = firebaseAuth.getCurrentUser();
userID=user.getUid().toString();
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setNAME(ds.child(userID).getValue(UserInformation.class).getNAME());
uInfo.setMAIL(ds.child(userID).getValue(UserInformation.class).getMAIL());
name.setText(uInfo.getNAME());
mail.setText(uInfo.getMAIL());
}
But now when someone publish a post, I can't get the user's data and show it in my activity for that information, when I try to open that activity, it stop working.
How can I solve that problem? I think is an error in the Reference, but I can't solve it.
I need to delete a user from the group chat once he clicks the exit group button. The above picture is how my database looks like
Suppose I want to delete the user with user_id: 15213
Here's my code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference groupMemberRef = database.getReference().child("group_users/"+chatGroup.group_id+"/"+userId);
groupMemberRef.removeValue();
While the code is technically correct, the entry isn't getting removed from the database.
I have never tried deleting a node the way you implemented. But I did as below:
DatabaseReference groupMemberRef = database.getReference().child("group_users/"+chatGroup.group_id+"/"+userId);
groupMemberRef.setValue(null);
See if it works..
To solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("group_users/" + chatGroup.group_id).orderByChild("user_id").equalsTo(15213);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ds.getRef().removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(valueEventListener);