I don't know how to retrieve all Uid. This is my data:
this is my snippet code to set the data:
reference = db.getReference("Attendance");
reference.child(id).child(uid).child.("Status").setValue(status);
any help thank you guys
To log all UIDs (and names) from the JSON:
reference = db.getReference("Attendance");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot idSnapshot: dataSnapshot.getChildren()) {
System.out.println(idSnapshot.getKey());
for (DataSnapshot userSnapshot: idSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("Status/xname").getValue(String.class));
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
Related
I'm using Firebase realtime database. Below is the structure
Problem Statement: Over a list of "Users", I need to delete child who's having coins = 0
So far I'm stuck here
DatabaseReference deleteRef = dbref.child("Users").child("coins");
Query deleteQuery = deleteRef.orderByValue().equalTo("0");
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
child.getRef().setValue(null);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Any help will be appreciated :)
You need to order the list by the child coins like here:
DatabaseReference deleteRef = dbref.child("Users");
Query deleteQuery = deleteRef.orderByChild("coins").equalTo("0");
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
child.getRef().setValue(null);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
I have the following Firebase Database :
I need to delete all the entries/database objects sharing the same "date_cours" type.
I tried the following method to delete all the entries sharing the same date_cours "10/09/2018", for example :
private void Delete_CR_Lessons(Date date) {
final String date_a_supprimer_string = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference("cours");
drTest.child("date_cours").orderByKey().equalTo("10/09/2018")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("Tag", "test1");
for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {
Log.i("Tag", "test2");
String key = postsnapshot.getKey();
dataSnapshot.getRef().removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG: ", databaseError.getMessage());
}
});
}//fin de la methode Delete_CR_Lessons
I have no error during the execution of the method.
In the Logs, I can see my Log "test1" but not the log "test2".
Does anyone know what I am missing ?
You are providing wrong path and than you are trying to delete wrong datasnapshot value for example try to use: postsnapshot.getRef().removeValue(); instead of dataSnapshot.getRef().removeValue(); because dataSnapshot doesn't point to the value which you want to delete. That is why you used for loop to get all value nodes from your database. Check code below:
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference("cours");
drTest.orderByChild("date_cours").equalTo("01/10/2018")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("Tag", "test1");
for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {
Log.i("Tag", "test2");
String key = postsnapshot.getKey();
postsnapshot.getRef().removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG: ", databaseError.getMessage());
}
});
The structure looks like this:
users
|----username1
|----uid:value
username2
|----uid:value
I'm trying to find the best way to get the username value based of the value of it's uid,
This need to be in Java code (Android), So far I found the following code:
Query uid = reference.child("users").orderByChild("uid").equalTo(uid);
uid.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
if (keys.equals(uid)) {
// uid found
} else {
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this code and create a model class to fetch value of Uid
DatabaseReference users= FirebaseDatabase.getInstance().getReference().child("users");
users.orderByChild("uid").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot post:dataSnapshot.getChildren()){
//Use a model class to fetch user id like below
UIDDetails u=post.getValue(UIDDetails.class);
String user_id=u.getUid();
//Here all values will be equal to youy required uid if exists more than one
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Please add below code into your file:
DatabaseReference users= FirebaseDatabase.getInstance().getReference().child("users");
final Query userQuery = users.orderByChild("uid");
userQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
map.clear();
if(child.getkey().toString().equalsIgnoreCase(uid)){
//Get the node from the datasnapshot
String myParentNode = dataSnapshot.getKey();
for (DataSnapshot child: dataSnapshot.getChildren())
{
String key = child.getKey().toString();
String value = child.getValue().toString();
map.put(key,value);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
databaseError.toException();
}
});
I am getting a null when I try to get data of the child node of a push id. My structure look like this
And this is my code for getting the message data
DatabaseReference mMessageDatabase = FirebaseDatabase.getInstance().getReference().child("private_messages");
mMessageDatabase.child(User1).child(User2).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
// getting null in message
String message = dataSnapshot.child("message").getValue(String.class);
Log.e(TAG, "CHAT_LOG: " + message);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Is there something wrong in my code or Am I missing a code there?
EDIT:
I try using a query to test if it gives me data. heres my code
messageDatabase.orderByChild("seen").equalTo("false")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String key = datas.getKey();
String seen = dataSnapshot.child(key).child("seen").getValue(String.class);
Log.e(TAG, "CHAT_LOG: Seen= " + seen);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Fortunately it gives me a data. THe only problem is getting the message. I used a query here and I need to give a specific value to it.
Try the following:
mMessageDatabase.child(User1).child(User2).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String message = datas.child("message").getValue(String.class);
Log.e(TAG, "CHAT_LOG: " + message);
}
}
}
Since the snapshot is at User2 then you can loop inside the direct child which is the push id and retrieve the value of child message
FirebaseDatabase.getInstance().getReference("Block").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getKey().equals(myUID)){
FirebaseDatabase.getInstance().getReference("Block").child(myUID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getKey().equals(list.get(position).getUid())){
FirebaseDatabase.getInstance().getReference("Block").child(myUID).child(list.get(position).getUid()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
if(snap.getValue().equals("1")){
call_method();
}else{
//code...
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
call_method();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
call_method();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This code works well, but it is very long so I am asking for the best way to write it ?
I want to check firstly if Block node has child equals to my UID if it is true then I want to check if my UID has child equals to specific UID if it is true then I want to get the value of block which is 0 in this example
How can I do that ? Thank you very much.
Just traverse to the path specified using child. You will get no data if the path doesn't exist.
FirebaseDatabase.getInstance().getReference("Block")
.child("MyUUID")
.child("specificUUID")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
// ... Do something
} else {
call_method();
}
// ... Other code
}
Try this:
Firebaseuser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Block");
ref.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exist()){
//checks first uid
if(dataSnapshot.hasChild(seconduid){
//checks if seconduid is there
for(DataSnapshot datas:dataSnapshot.getChildren()){
String blocks=datas.child("block").getValue().toString();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This will check the first uid if(dataSnapshot.exist()) after that it will check if that uid node has the second uid that you provide if(dataSnapshot.hasChild(seconduid){ then you will iterate and get the node block