How to read multiple random key data from firebase android - android

I am trying to read data from firebase in my android app. This is how it looks.
I want to read the marked data. I am able to read the key of categories child. But unable to read the ids values.
Here's my code:
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
String name = ds.getKey();
String ids = ds.child("ids").getValue(String.class);
Log.i(name, "onDataChange: "+ids);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

You could not connect to the root you want. For example, tcat2, you can modify the ids.
You must specify the path from which you want to read.
You can try this :
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
String name = ds.getKey();
for(DataSnapshot ds1 : ds.child(name).child("ids").getChildren()){
String ids = ds1.getValue(String.class);
Log.i(name, "onDataChange: "+ids);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I hope I helped you.

Related

Retrieve complex nested data from firebase

I have the data in firebase as given below:
I want to get the Customer and Device details for each of the Form ID. The Form IDs are generated uniquely by firebase.
I just somehow need to get access to these Form IDs.
I have set the databaseReference as follows:
databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userID).child("forms");
Then I have tried the following code to retrieve the required data:
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot formsSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot formIDSnapshot : formsSnapshot.getChildren()) {
Device device = formIDSnapshot.getValue(Device.class);
if (device != null) {
// get and use the data
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Update:
I am now successfully able to retrieve the form IDs, thanks to the answer by #Frank.
Now, to get the Device and Customer details I have to write the following code inside the ValueEventListener after getting the form IDs:
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot formsSnapshot : dataSnapshot.getChildren()) {
String formID = formsSnapshot.getKey(); //Retrieving the formID here
//New code addition
// to get the reference to device details node
databaseReference.child(Objects.requireNonNull(formID)).child("Device details").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Device device = dataSnapshot.getValue(Device.class);
if (device != null) {
// required device details here
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
// to get the reference to customer details node
databaseReference.child(Objects.requireNonNull(formID)).child("Customer details").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Customer customer = dataSnapshot.getValue(Customer.class);
if (customer != null) {
//required customer details here
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
I understand that this is a messy code and I would like to get some cleaner approach for the same.
You can get the key of a node by calling DataSnapshot.getKey(). So to get your form IDs:
databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userID).child("forms");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot formsSnapshot : dataSnapshot.getChildren()) {
String formID = formsSnapshot.getKey();
Device device = formsSnapshot.getValue(Device.class);
if (device != null) {
// get and use the data
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException(); // never ignore errors
}
});
There is no need for the nested loops. Simply get the children list and loop over it once. At every iteration, cast dataSnapshot to FormID class and afterwards just use its properties.
i would also suggest that you use SingleValueEventListener instead of ValueEventListener if you do not need to observe the data all the time

Android Firebase push and get Array List

I have a questions how could i push array list to my freebase data base
and also read it to add a new element , then push it again.
In the above photo you can see i have added array list joinedStudents
and i need to read it and push new elements to it
I've tried the following
DatabaseReference requestsRef = FirebaseDatabase.getInstance().getReference();
requestsRef.child("requests").child(requestID).child("joinedStudents");
requestsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot d : dataSnapshot.getChildren()) {
String jR = d.getValue(String.class);
joinedStudents.add(jR);
}
joinedStudents.add(studentID);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, "error fetching data from firebase");
}
});
for (String string : joinedStudents) {
requestsRef.child("requests").child(requestID).child("joinedStudents").setValue(string);
}
but it's not working
I believe you have your references slightly mixed up. Try something like:
DatabaseReference joinedStudentsRef = FirebaseDatabase.getInstance().getReference().child("requests").child(requestID).child("joinedStudents");
joinedStudentsRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot d : dataSnapshot.getChildren()) {
String jR = d.getValue(String.class);
joinedStudents.add(jR);
}
joinedStudents.add(studentID);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, "error fetching data from firebase");
}
});
for (String string : joinedStudents) {
joinedStudentsRef.child("1").setValue(string); // setValue is for key, value
// but you have an array
}
Where your reference is now the direct node that you want to monitor/read/write

How to get key of elements in firebase?

I am an absolute beginner in Android and Firebase. Forgive me for my post. I want to get the key element that is under "node" eventos. Till I have done the following code, but I am no able to get the key. Please help me, I am doing my college project.
DatabaseReference mDataBase = `FirebaseDatabase.getInstance().getReference();`
DatabaseReference usuarios = mDataBase.child("usuarios");
DatabaseReference eventos = usuarios.child("eventos");
eventos.orderByValue().equalTo("eventos").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
String key = snap.getKey();
System.out.println("LAS CLAVES SON: "+key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
enter code here
}
});
I want to get the key to update each element and delete.
you have to just remove your some code :-
eventos.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
String key = snap.getKey();
System.out.println("LAS CLAVES SON: "+key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
enter code here
}
});

How to get specific child data from firebase

I have need to access a specific child from node, i have only "available key" of the specific child not complete path.
Since you know the key of the child you're looking for, you can query on that:
DatabaseReference schoolsRef = FirebaseDatabase.getInstance().getReference().child("School");
Query query = schoolsRef.orderByKey().equalTo("MZ3bW5kLJAQorgnZbYiTaOoWWSG2");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
String email = userSnapshot.child("email").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
Note that you can't use orderByChild("id") as explained in my answer
here: Firebase Query Double Nested.
Just add this code:
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("Email").getValue().toString();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
try this :-
DatabaseReference mUserNameReference = FirebaseDatabase.getInstance().getReference("School").child("id").child("id");
mUserNameReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue().toString());
//get all the key value like this
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
If you have key for that child but not complete path first get key of parent node in list like below I did.
Arraylist<String> keyList =new Arraylist();
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
String value= childSnapshot.getValue(String.class);
keyList.add(key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
now use for loop or foreach loop to check if that node contains available child key.
for(String key:keyList){
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(key).orderByChild("id").equal(availableKey);
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
Object object= childSnapshot.getValue(Object.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
hope this will help you..

How to retrieve all data from nested nodes in firebase database

I would like to get all the values from my database. But the problem is that I can't make the database reference correct or the for loop of the datasnapshot in order to get all the values. The outcome was always null and without errors.
Here is my code:
databaseReports.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
reportList.clear();
for(DataSnapshot userSnapshot : dataSnapshot.getChildren()){
Reports reports = userSnapshot.getValue(Reports.class);
reportList.add(reports);
}
ReportList adapter = new ReportList(ViewReports.this, reportList);
listViewReports.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
I want to get the value of the all the date nodes.
here is my databaseReference
FirebaseDatabase.getInstance().getReference("REPORTS")
Using this produce and empty list because of the referencing.
databaseReports = FirebaseDatabase.getInstance().getReference("REPORTS/05-10-2017");
But this one only shows the data under 05-10-2017
please help me get all the data from sub nodes under REPORTS.TIA
You may try this...
databaseReports.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
reportList.clear();
for(DataSnapshot ymdSnapshot : dataSnapshot.getChildren()){
Log.d("ymdSnapshot", ymdSnapshot.getKey().toString());
for(DataSnapshot repSnapshot : ymdSnapshot.getChildren()){
Reports reports = repSnapshot.getValue(Reports.class);
reportList.add(reports);
}
}
ReportList adapter = new ReportList(ViewReports.this, reportList);
listViewReports.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}
You may try this... partII
databaseReports.child("05-09-2017").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
reportList.clear();
for(DataSnapshot repSnapshot : dataSnapshot.getChildren()){
Reports reports = repSnapshot.getValue(Reports.class);
reportList.add(reports);
}
ReportList adapter = new ReportList(ViewReports.this, reportList);
listViewReports.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}

Categories

Resources