How to retrieve all data from nested nodes in firebase database - android

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) {
}
}

Related

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

Retrieve first child from a parent in Firebase

I am working on an Android application. I want the first child (first UID) of the parent 'support' and store it in a String variable. How do I get the value of the first UID from the list?
I tried one approach. It doesn't work though.
#Override
public void onDataChange(DataSnapshot dataSnapshot1) {
if (dataSnapshot1.exists()) {
String futureUID = "";
for(DataSnapshot futureUIDdatasnapshot:dataSnapshot1.getChildren() ){
futureUID = futureUIDdatasnapshot.getKey();
break;
}
/*Getting the first UID from the list of UID's in queue in 'future'*/
futureUID = dataSnapshot1.getChildren().iterator().next().getKey();
/*Moving a card from 'future' to 'serving'*/
societyServiceUIDReference.child(FIREBASE_CHILD_SERVING).child(futureUID).setValue(FIREBASE_ACCEPTED);
/*Removing the UID from 'future' after it is placed in 'serving'*/
societyServiceUIDReference.child(FIREBASE_CHILD_FUTURE).child(futureUID).removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
NOTE: 'futureUID' is the UID I want
Try the following:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("support");
Query queryUid=ref.orderByKey().limitToFirst(1);
queryUid.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String key=datas.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
.orderByKey().limitToFirst(n) is what does the trick. It orders the query results by key and returns only the first n results; in this case 1
mDatabase.getChild("support").orderByKey().limitToFirst(1)
.addListenerForSingleValueEvent(new ValueEventListener () {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot supportItem: dataSnapshot.getChildren()) {
String futureUID =supportItem.getKey();
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Catch your error here
}
});
see Work with Lists of Data on Android

How to handle a json returning data objects with firebase in android

I have an android application which does the following:
com.google.firebase.database.Query query = mUsersRef
.orderByChild(USER_TABLE_EMAIL)
.startAt(searchTerm)
.endAt(searchTerm + FirebaseConstants.SEARCH_ESCAPE);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot d = dataSnapshot;
getView().showSearchResults();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: something?
}
});
I was wondering what the simplest way to convert dataSnapshot into User objects was. Do I need to get returned json and do some form of conversion? or is there a better way?
You can convert your data snapshot into an object as shown below:
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot : dataSnapshot.getChildren()){
User user = snapShot.getValue(User.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: something?
}
});

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?

Displaying the database nodes from Firebase in a list in Android

A simple question:
Referring to my database image, I want to get the values of the children of node "user" in a list in an Activity, i.e. values "a" and "m" should be displayed in the list.
What should I use and how?
Please try this code:
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("user");
yourRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> usersList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String userName = (String) ds.getKey();
usersList.add(userName);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
This is the way in which you'll have in your list, a, m ans so on.
Hope it helps.
You can get the info from the docs here
List<User> allUsers = new ArrayList<>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("user");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
allUsers.clear();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
allUsers.add(userSnapshot.getValue(User.class));
}
yourRecyclerViewAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Users failed, log a message
Log.w(TAG, "loadUser:onCancelled", databaseError.toException());
// ...
}
});

Categories

Resources