How to get newly created User's userId (cannot be current user)? - android

I am begginer in Firebase and my problem is:
I have to get one user id of any of the recently added users but the user can't be the current user.
Until now I have done this but it isnt working.
DatabaseReference databaseusers = FirebaseDatabase.getInstance().getReference().child("Users");
Query query=databaseusers.limitToLast(50);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(!snapshot.getValue().toString().equals(currentuseruid)){
player2uid=snapshot.getValue().toString();
}
}

Use getKey() to get a node's key
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (!snapshot.getKey().equals(currentuseruid)) {
player2uid = snapshot.getKey();
}
}
Hope this helps :)

Related

firebase orderByChild() with specified child key doesnt work

I'm trying to get data which has current user uid and sort by time, but somehow my query brings data which has other uid as well
This is my query
FirebaseDatabase.getInstance().getReference().child("posts").orderByChild("uid_timestamp/"+uid).limitToLast(12)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
items.clear();
for (DataSnapshot item : dataSnapshot.getChildren()) {
items.add(item.getValue(PostModel.class));
Log.d("checking","writerUid "+item.getValue(PostModel.class).writerUid);
}
This is my database
So the writerUid is in uid_timestamp with the time. Am I using wrong child key? I need your help!

How can i retrieve a specific data from nodes in firebase and display to lisltview

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

Optimal way to get last posts of user's friends in Firebase

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.)

android firebase database: how to get all nodes that are in a set of values

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.

How to get data from a specific reference in firebase?

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.

Categories

Resources