I need to get the key of each event that match the users ID's that I have looped before in another table, so, I have this table where the current userID that is logged in has a list of users that will assist to a certain event
Im logged in as the user MUTzo13NuJRlbbkZCDp2nCdHkIn1 , then I just get all the users below that id , so I know which one I invited
I have done that with this
mDatabase.child("userEvent").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot snapshot1 : dataSnapshot.getChildren()){
final String users = snapshot1.getKey();
...
So , now my string of users has all the users from the current logged in one.
Now, since I have all those users that belongs to the logged in one , I want to loop another table, find the matching users and get the key (the event ID in which those users has not assisted)
Table events
I need to get for example the ID LT9d-sJCmMBUIW14AG8 if the users looped before are inside of it, so I just tried this
mDatabase.child("userEvent").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot snapshot1 : dataSnapshot.getChildren()){
final String user = snapshot1.getKey();
//Now I go to the events table and try to get the event ID if those users are inside of them
mDatabase.child("events").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot snapshot2 : dataSnapshot.getChildren()){
if(snapshot2.equals(user))
Log.d(TAG, "onDataChange: "+snapshot2.getKey());
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
But it does not work, instead getKey returns the name node events, instead of the name of the node that the user belongs.
Any tip ?
EDIT:
Thanks Peter for the answer, I'm having a trouble that is the users are beign matched with events that they dont belong to
So , the problem is that its matching to events users that It does not belong to
Try the following:
mDatabase.child("events").orderByChild(user).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot snapshot2 : dataSnapshot.getChildren()){
String keys = snapshot2.getKey();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You need to add the user String that you retrieved in the first listener inside orderByChild() and then you will get a snapshot that contains that child. After looping you will be able to get the parent key of that child.
Related
In Firebase I have the "users" where I save all users' information.
Now I want to show someone I like in the "Favor".
This is "Favors"
In "Favor", only my ID and people ID I added to the favor list
And this is "users":
In "users", I have all users' information inside people's ID
And this is my code to show all users
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
users.clear();
for(DataSnapshot snapshot1: snapshot.getChildren()){
User user= snapshot1.getValue(User.class);
if(!user.getUid().equals(FirebaseAuth.getInstance().getUid()) && hasChat != null)
users.add(user);
}
binding.recyclerView.hideShimmerAdapter();
usersAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
And this code i'm trying to show favor list, but it's show blank:
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
users.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
User user = snapshot1.getValue(User.class);
if (!user.getUid().equals(FirebaseAuth.getInstance().getUid()) && user.getUid().equals(database.getReference().child("Favors").getKey()))
users.add(user);
}
binding.recyclerView.hideShimmerAdapter();
usersAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
I create database using curentdate (1-25) as parent id and using room (08-00_11-00_karpet1-) as child id. inside this database are user information that order that room (child id) on that date (parent id).
Question 1
using this layout design, how do i disable button that have been ordered so that indicate that room no longer available.
(example. user order karpet 14-17 on 1-28, when user create order database will update database datajadwal based on date (parentid) >> room (childid) >> userinformation (email.name.phone.etc) )
Question 2
each date have 5 rooms. when specific date click, room button will disabled according to the data on datajadwal and on that date (parentid), if there are no room data (childid) that means that room are available.
my situation =
i don't know hot to get the childid
private void datajadwalupdate(){
databseJadwal = database.getReference("datajadwal").child(haritanggalOrder);
databseJadwal.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
final String rooms = dataSnapshot.getKey();
databseJadwal.child(haritanggalOrder).child(rooms).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
this is where I'm, but got error on a null object reference
If I understand your question correctly, you will need all data under datajadwal to disable the correct buttons. In that case you can use a listener like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("datajadwal");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dateSnapshot: dataSnapshot.getChildren()) {
String date = dataSnapshot.getKey();
for (DataSnapshot bookingSnapshot: dateSnapshot.getChildren()) {
String booking = bookingSnapshot.getKey();
// TODO: parse the booking into the time slot and room name
String timeslot = ...
String room = ...
// TODO: find the button for the room name and time slot and disable it
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
You need to iterate throught the childs of the snapshot and check the data, if there is user data then disable it.
DatabaseReference ref = rootRef.child(id).child(date);
ref.addValueEventListener(...
onDataChange...{
for (Snapshot child: dataSnapshot.getChildren()) {
//set the names as you are doing it
boolean occupied = child.hasChild("email)
button.setEnabled(!occupied)
}
}
You should create a POJO representing your entity and get the value from the snapshot child with getValue(YourPOJO.class)
I want to delete the Firebase database child with this follow code when I click the first item in a list in an app, but I can't. What's wrong?
Query removeCalendar = mCalendarDatabaseReference.limitToFirst(1);
removeCalendar.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String remCal = dataSnapshot.toString();
mCalendarioDatabaseReference.child(remCal).removeValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Queries return a list of possible locations where the query might be satisfied, you'd need to iterate through your dataSnapshot to access those locations. Moreover, this :
String remCal = dataSnapshot.toString();
is not going to print the String value of this snapshot. If you want to get the string value of a dataSnapshot it should be:
String remCal = dataSnapshot.getValue(String.class);
If you want to get the reference of a datasnapshot just use getRef(), you don't have to access the original reference.
Query removeCalendar = mCalendarDatabaseReference.limitToFirst(1);
removeCalendar.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
child.getRef().setValue(null); //deleting the value at this location. You can also use removeValue()
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I solve the problem with this code:
Query removerCalendario = mCalendarioDatabaseReference.limitToFirst(1);
removerCalendario.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ds.getRef().removeValue();
}
}
You can do mCalendarioDatabaseReference.child(remCal).setValue(null);
How can I get random key from Firebase realtime database that are stored in list?
Try this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("inspirational");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
First you need to go the dataSnapshot inspirational, then iterate inside of it and, this will give you the random keys String keys=datas.getKey();
I am trying to build a simple friend list, where the user can create some friend groups and each friend group can have a lot of friends, inside the friend group the user can add new friends by contact(inserting the phone number on a editText) the user is added in the list(I use recycleViews), each time I insert a new user in a friendList group in firebase it is added inside the group with the specific name(name can't be repeated for groups) with the userID and the value = true for that user, this is a representation of my firebase:
image the 't' refers to true
what I need, is to display all the users for a specific group, as long as I safe just the id inside my group, I need to get the entire user from that id, but not just 1 user I need to get them all.
So I did this:
private void prepareFriendList() {
myRef.child(id).child("FriendLists").child(group).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
currentFriends.add(snapshot.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
myRef.child(id).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
for(String item: currentFriends){
if (snapshot.getKey().equals(item)) {
usersList.add(user);
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mAdapter.notifyDataSetChanged();
}