How to get all users from firebase database that send friend request - android

I want to retrieve all the users that have 'request_type: receive' only. How I gonna do that in the android studio? It's for the chat app for request fragment. I want to display in RecyclerView all friend request only.
Image

Check out The query below. It can help you.
Query query = FirebaseDatabase.getInstance().getReference().child("Friend_req").orderByChild("request_type").equalTo("received");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// Do your job
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

To solve this, you need to loop through your Friend_req node using getChildren() method twice:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Friend_req");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String requestType = ds.child("request_type").getValue(String.class);
if(requestType.equals("receive")) {
Log.d("TAG", "Request received!");
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);

Related

how i get all data from firebase realtime database

I have a database with this schema:
I tested this code but I can't get any msg from the database. I would appreciate your help.
private void Add_Chat(final DataSnapshot dataSnapshot) {
root.child("ChatSpace").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
String nom=dataSnapshot.child("ChatSpace").child("msg").getValue(String.class);
System.out.println(nom); //prints "Do you have data? You'll love Firebase."
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Assuming that ChatSpace is a direct chlid of your Firebase database root, to get all messages please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatSpaceRef = rootRef.child("ChatSpace");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String msg = ds.child("msg").getValue(String.class);
Log.d("TAG", msg);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
chatSpaceRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
bbbName
bbbName
Name

Android - Firebase getting all the data that is on another table

In practice, people can send requests to each other. What I want to do is to show all the requests sent to one person in that person's profile. I can show all users information with firebase adapter in recyclerview but I could not show only request senders. How can I do? I'd be very happy if you could give an idea to us.
As I understand you want to show the children names of the parent you can use this code.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Friend_req").child("recieved").child("id_of_reciepent");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String request_sender_id = (String) ds.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);
To display the names only of the friends who sent a friend request, you need to query your database twice. Please use the following code in order to achieve this.
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Friend_req").child("recieved").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference userRef = rootRef.child("Users").child();
ValueEventListener eventListener = new ValueEventListener(key) {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String name = dSnapshot.child("Name").getValue(String.class);
Log.d("TAG", name);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
Your output will be:
Bob
Second Friend

Followers structure firebase android

I want to implement followers functionality in my android application. 'First' image refers to the list of user i followed. 'second' image have all ids of all the posts that users post and 'third' image havin post details. all i want to do is to load only the post of those users that i've followed.
What i am doing is, i get emails from followers node and by using indexed recyclerview i load the data but its not working because recyclerview is not getting dynamic reference. Any sugested solution ?
first
second
third
In order to achieve this, you need to query your database for three times like this:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String userEmail = firebaseUser.getEmail();
String encodedUserEmail = userEmail.replace(".", ",");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userEmailFromFollowersRef = rootRef.child("followers").child(encodedUserEmail);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String followerEmail = ds.getKey();
DatabaseReference my_postsRef = rootRef.child("my_posts").child(followerEmail);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String postId = dSnapshot.getKey();
DatabaseReference postsRef = rootRef.child("posts").child(postId);
ValueEventListener vel = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()) {
String postText = child.child("postText").getValue(String.class);
Log.d("TAG", postText);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(vel);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
my_postsRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userEmailFromFollowersRef.addListenerForSingleValueEvent(eventListener);

Firebase query in tree with depth and multiple children

I have a problem regarding querying the firebase database using a value and getting the specific node. My schema is shown here:
In my schema 'workLocations' belongs to an 'excavationWorks', and 'excavationWorks' belongs to an 'excavationLists'.
That means that the path to a specific workLocation is excavationLists/excavationWorks/workLocations/(specific workLocation)
The problem that I have is that I want to query the workLocations node by the location value (let's say London) and get the parent node (the red circled key which is the key of the specific workLocation).
I have search and read many posts but I haven't managed to make it work.
My code looks like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("workLocations").orderByChild("location").equalTo("London");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot workLoc : dataSnapshot.getChildren()) {
// do something with the individual "issues"
Log.d(TAG, workLoc.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you
To achieve this, you need to query your database twice like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference workLocationsRef = reference
.child("excavationLists")
.child("excavationWorks")
.child("workLocations");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String key = ds.getKey();
Query query = workLocationsRef.child(key).orderByChild("location").equalTo("London");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
String description = snapshot.child("description").getValue(String.class);
Log.d("description", description);
String partentKey = snapshot.getRef().getParent().getKey();
Log.d("partentKey", partentKey);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
workLocationsRef.addListenerForSingleValueEvent(valueEventListener);

Firebase query in query

I am trying to make multiple queries to my Firebase database, but it seems I am doing something wrong, and I just can't seem to understand what.
My code is this:
database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("Tbl1");
Query tbl1Query = ref.orderByChild("type");
tbl1Query .addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Tbl1Item item = singleSnapshot.getValue(Tbl1Item .class);
tbl1List.add(item);
}
DatabaseReference ref = database.child("Tbl2");
Query tbl2Query = ref.orderByChild("status");
tbl2Query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
tbl2Item item = singleSnapshot.getValue(tbl2Item.class);
tbl2List.add(item);
}
DatabaseReference ref = database.child("Tbl3");
Query tbl3Query = ref.orderByChild("status");
tbl3Query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
tbl3item item = singleSnapshot.getValue(tbl3item.class);
tbl3List.add(item);
}
SortTableData();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The problem is that the app crashes though I get no error in console.
What I am trying to obtain, is create 3 lists of objects with data from table1, table2 and table3 and then call a method that will process that data.
If I remove the 2nd and 3rd queries, everything works fine, but I don't have all the needed data... so what I am doing wrong?

Categories

Resources