I'm trying to retrieve the child below "ticket" with the key in between but not able to do it. Method getKey() is return "ticket" instead of the key.
private void getSpecificTicketFromFirebase() {
Timber.d("Inside Pull Data %s ",firebaseManager.getFireBaseUser().getUid()) ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("user").child(firebaseManager.getFireBaseUser().getUid()).child("ticket");
FirebaseDatabase.getInstance().getReference().addValueEventListener(new ValueEventListener() {
int i=0;
#Override public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
passengerData = data.getValue(PassengerViewModel.class);
passengerViewModel.add(passengerData);
Timber.d("Passenger Name %s index %s key %s",passengerViewModel.get(i).getFromStationName(),i,data.child("passengerName"));
i++;
}
}
#Override public void onCancelled(DatabaseError databaseError) {
Timber.e(databaseError.getDetails());
Timber.e(databaseError.getMessage());
Timber.e(databaseError.toException());
}
});
}
If you only want to get those objects of PassengerViewModel class, there is no need to use the child() method. Just remove this call: .child(ref.getKey()) and your code will work perfectly fine.
If you want to pass that pushed key to the child() method, you need to store it first into a variable. To achieve this, please use the following code:
String key = ref.push().getKey();
Once you have this key, you can use it in any reference.
First of all Thank you #AlexMamo....
Here is the correct way to do it.....
private void getSpecificTicketFromFirebase() {
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference().child("user").child(firebaseManager.getFireBaseUser().getUid()).child("ticket");
ref.addValueEventListener(new ValueEventListener() {
#Override public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Timber.d("Key %s",data.getKey());
}
}
#Override public void onCancelled(DatabaseError databaseError) {
Timber.e(databaseError.toException());
}
});
Related
This is the structure of my database:
I want to get all the outcome value, but I need to loop the faculty child first and the exam child before I able to use the getValue() in outcome. How can I loop the faculty child and exam child in the same time?
This is my code, but I don't know how to get to the Exam child and loop it again to get the outcome value.
dbOutcome.child("sample_list").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You can use the models classes to retrieve this.
Make model classes and get data in them
public class Exam {
pirvate String outcomes;
// create constructor and getter setter
}
create Faculty class
public class Faculty {
List<Exam> exams
// create constructor and getter setter
}
get Data from snapshot in onDataChange like this
public void onDataChange(DataSnapshot dataSnapshot) {
List<Faculty> faculties = (ArrayList<Faculty>) dataSnapshot.getValue();
}
I want to get all the outcome value
Assuming that the sample_list node is a direct child of your Firebase root, to solve this, you need use getChildren() method twice:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("sample_list");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String outcome = ds.child("outcome").getValue(String.class);
Log.d("TAG", outcome);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
pass
fail
pass
fail
I have this item in the database
And I have a method for removing items, but is not working, this is the method
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
It gets the reference well, but the method setValue(null) is doing nothing (I've also tried the removeValues().
I don't get any exception or some kind of response, I hope you can help me.
Thanks!
I beleive you have a model class that inserts your items into the database.
so what you have to do is to use that class to reference the object directly and perform the required action on it. Something like this.
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//This shouldn't be there
//dataSnapshot.getRef().setValue(null);
//instead use the modelclass you used inserting data, to reference the object
Modelclass mode = dataSnapshot.getValue(Modelclass.class);
/** Note this line indicates the getter and setter method for the particular object that is being referenced**/
mode.setItem(null)
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
When you execute a query against the Firebase Database, there will potentially be multiple results. You're using a ValueEventListener, so the snapshot contains a list of all results. Even if there is only a single result, the snapshot will contain a list of one result.
You need to loop over DataSnapshot.getChildren() to get the individual items matching your query:
public void deleteBag(String bagUid, final FirebaseDeleteBaglListener listener) {
Query query = dbReference.child(FirebaseChild.bags.name()).child(bagUid);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
child.getRef().removeValue();
}
listener.notifyBagDeleted();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...
reference = FirebaseDatabase.getInstance().getReference("Users");
Query query = reference.orderByKey().equalTo(task.author_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase Structure
You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.
reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
fullName = dataSnapshot.getValue(User.class).getFullName();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I'm looking to search through my Firebase database and find a field:value pairing that matches my query, and then return either that parent's key, or the parent object so that I may grab other information as well.
The Firebase database looks something like this:
Events{
-KiXlIGhB6k-HpCKfO3n{
name:"Breakfast at Tiffany's",
owner:"Tim",
startTime:{
startHour:1,
startMinute:30
},
...
},
-dFgfh8Efa-Hpwe6Goqp0{...}
}
I'm currently attempting:
public void importSchedule(String ownerName){
DatabaseReference events =
FirebaseDatabase.getInstance().getReference("Events"); //Inside the Events list
Query allOwnersEvents = events.equalTo(ownerName); //Find events equalTo ownerName provided
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
// This is where the parent's could be queried, all events belonging to an "owner" should be cycled through
}
}
public void onCancelled(DatabaseError databaseError) {}
});
However when placing a break-point inside the for loop, it is never triggered. I'm wondering if I'm attempting the query correctly or if there's an easier way to accomplish this.
It's never triggered because your DatabaseReference is wrong. When you query, you are missing a child. In order to have the correct DatabaseReference please use this code:
DatabaseReference events = FirebaseDatabase.getInstance().getReference("Events")
events.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String eventKey = ds.getKey(); //parent key
DatabaseReference allOwnersEvents = FirebaseDatabase.getInstance().getReference("Events").child(eventKey);
allOwnersEvents.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String ownerName = dataSnapshot.child("ownerName").getValue(String.class); //do what you want with ownerName
}
public void onCancelled(DatabaseError databaseError) {}
});
}
}
public void onCancelled(DatabaseError databaseError) {}
});
In which eventId is the unique id generated by the push() method. Hope it helps.
I am trying to solve how to get a value from my DatabaseReference, but doing so gives me the full address when I want to put it in a TextView
This is the reference:
public static DatabaseReference getUsernameRef(String email){
return FirebaseDatabase.getInstance().getReference("nom_usuarios");
These are the values that I want to rescue:
i tried this:
mPosti.setUsername(FirebaseUtils.getUsernameRef(FirebaseUtils.getCurrentUser().getEmail()).toString());
But instead of receiving the value, I get the address of the database.
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("nom_usuaios").getRef();
database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you will the value in datasnapshot
for (DataSnapshot dataSnapshot : dataSnapshot.getChildren()) {
list.add(dataSnapshot.getValue());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});