How To Retrieve Specific Node From Firebase Database In Android
Solution 1:
You're creating a query in this line:
QueryqueryRef= myFirebaseRef.orderByChild("fullName");
Like that the query orders the child nodes by their fullName
value. But it doesn't yet limit what child nodes are returned.
To limit the nodes, you also have to filter. e.g.:
QueryqueryRef= myFirebaseRef.orderByChild("fullName").equalTo("gooner");
You can also get a range of nodes, by filtering with startAt
and/or endAt
instead of equalTo
.
As Kato commented:
It looks like there is a superfluous level of children. The data is structured as
saving-data/fireblog/<record id>/fluff/...actual data..
. and the fluff layer needs to be removed for those queries to work. You can't query children of children.
Solution 2:
If you want to get value of specif node or child node like this
Here if you want to get child node(address) value. You can get it in this way
FirebaseDatabase database;
DatabaseReference myRef;
myRef = database.getReference();
finalDatabaseReferenceorders_Reference= myRef.child("Order");
orders_Reference.addValueEventListener(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
if(data.getKey().equals("address")){
StringorderNumber= data.getValue().toString();
Log.d("Specific Node Value" , orderNumber);
}
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
Solution 3:
This is another way to retrieve single value from firebase database using Hashmap.
ArrayList<HashMap<String,String>> AllUserscourselist;
String SelectedCourseUserUId;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("User_course_Details").addValueEventListener(new ValueEventListener() {
@Override
publicvoidonDataChange(DataSnapshot dataSnapshot) {
// emergencyContactsList = new ArrayList<>();
AllUserscourselist = new ArrayList<HashMap<String, String>>();
if(dataSnapshot.exists())
{
for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
{
for (DataSnapshot courseUID : postSnapShot.getChildren()){
UsercourseDetails usercourseDetails = courseUID.getValue(UsercourseDetails.class);
HashMap<String, String> map = new HashMap<String, String>();
String user_id = postSnapShot.getKey();
String course_id = usercourseDetails.getcourse_id();
String course_type = usercourseDetails.getcourse_type();
String course_brand = usercourseDetails.course_brand;
String course_number_plate_url = usercourseDetails.course_number_plate_url;
map.put("user_id", user_id);
map.put("course_id", course_id);
map.put("course_type", course_type);
map.put("course_brand", course_brand);
map.put("course_number_plate_url", course_number_plate_url);
AllUserscourselist.add(map);
}
// AllUserscourselist.add(new UsercourseDetails(usercourseDetails.getcourse_type(),usercourseDetails.getcourse_brand(),usercourseDetails.getcourse_number_plate_url(),usercourseDetails.getcourse_id()));
}
Log.e("AllUserscourselist",""+AllUserscourselist);
courseIdList = new ArrayList<String>();
for (int i = 0; i < AllUserscourselist.size(); i++) {
String course_id_list;
course_id_list = AllUserscourselist.get(i).get("course_id")+" "+ AllUserscourselist.get(i).get("user_id");
courseIdList.add(course_id_list);
}
if(courseIdList.size()>0 && courseIdList!=null) {
for (int i = 0; i < courseIdList.size(); i++) { //used
String arr[] = courseIdList.get(i).split(" ");
if (arr[0].equals(coursenumber)) {
SelectedcourseUserUId = arr[1];
getUserEmergencyContacts(SelectedcourseUserUId);
break;
}
}
}
}else{
// NavigationActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_in_left);
}
}
@Override
publicvoidonCancelled(DatabaseError databaseError) {
}
});
Solution 4:
Instead of getting all the nodes and then iterating it to get nodes based on value, just trigger the query provided by firebase.
privatevoidreadAllRequestedFormFromFirebaseDb(){
finalFirebaseDatabasedatabase= FirebaseDatabase.getInstance();
DatabaseReferenceref= database.getReference("App_DB");
DatabaseReferencechildRef= ref.child("AllRequest");
QueryqueryRef=
childRef.orderByChild("fullName").equalTo("Jay");
queryRef.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(@NonNull DataSnapshot snapshot) {
// getting listfor(DataSnapshot dataSnapshot: snapshot.getChildren()){
QueryFormModelpost= dataSnapshot.getValue(QueryFormModel.class);
queryFormModelArrayList.add(post);
/*the above list will have record only
with the provided fullName as Jay*/
}
}
@OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
Log.e("DetailsActivity", "onCancelled:readAllSubmittedRequestFromFirebaseDb: "+databaseError );
}
});
}
Click here and find the beautifully written post about this topic
Post a Comment for "How To Retrieve Specific Node From Firebase Database In Android"