How to retrieve specific list of data from firebase
If you using addValueEventListener
for retrieve all university from Firebase
List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
universityList.clear();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
University university = postSnapshot.getValue(University.class);
universityList.add(university);
// here you can access to name property like university.name
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
Or if you using addChildEventListener
List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
University university = dataSnapshot.getValue(University.class);
universityList.add(university);
Log.i(TAG,"add university name = " + university.name);
...
}
...
}
Extracting Individual Values From Firebase DataSnapshot
If you already have a snapshot, you can address children with child()
:
public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getKey()); // Food
System.out.println(snapshot.child("-key1").getValue()); // true
}
You can also loop over the children:
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
System.out.println(child.getKey()); // "-key1", "-key2", etc
System.out.println(child.getValue()); // true, true, etc
}
}
How to retrieve all the user details from database in firebase
Your question isn't very clear. By user data, if you mean the user
object of the other users, then simply put, you can not obtain the user
object of the other users regardless of their sign-in method. You can only obtain your own user
object by the FirebaseAuth.getInstance().getCurrentUser()
method.
Having said that, if the data of the users is stored in the realtime database, you can obtain it in the same way that you obtain data from other nodes. Please note, though, that the privacy settings of your app will determine if you can read other users' data or not.
It would help if you show your database structure in the question.
Edit After Viewing Database Structure :-
Alright, from what I understand, this is quite a basic problem. What you need is a childEventListener
to retrieve all the children ( in your case - users ) of the trace-5fa8c
node.
The code for that is as follows :-
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("trace-5fa8c");
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
userinfo user = dataSnapshot.getValue(userinfo.class);
Log.d(TAG, "showData: name: " + user.getName());
Log.d(TAG, "showData: Mobile: " + user.getMob());
Log.d(TAG, "showData: Vehicle: " + user.getVehicle());
Log.d(TAG, "showData: Address: " + user.getaddress());
//Do whatever further processing you need
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
Also, even though this is not a part of your question, I'd like to add that with each onChildAdded(datasnapshot)
, you can get a userInfo
pojo. So, in your showData()
method, instead of creating a new Arraylist
each time, maintain a single Arraylist<UserInfo>
and keep adding the child pojo to it each time a child is added. This ArrayList
object will be the dataset for your ListView
object.
How to get all child's data in firebase database?
First retrieve your users datasnapshot.
//Get datasnapshot at your "users" root node
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get map of users in datasnapshot
collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
Then loop through users, accessing their map and collecting the phone field.
private void collectPhoneNumbers(Map<String,Object> users) {
ArrayList<Long> phoneNumbers = new ArrayList<>();
//iterate through each user, ignoring their UID
for (Map.Entry<String, Object> entry : users.entrySet()){
//Get user map
Map singleUser = (Map) entry.getValue();
//Get phone field and append to list
phoneNumbers.add((Long) singleUser.get("phone"));
}
System.out.println(phoneNumbers.toString());
}
This listener will only retrieve the datasnapshot when explicitly called. Consider storing a list of numbers under an "allPhoneNumbers" node in addition to your users node. This will make your datasnapshots lighter and easier to process when collecting all numbers. If you have say hundreds of users, the "users" datasnapshot will be way too big and the "allPhoneNumbers" list will be far more efficient.
The above code was tested on your sample database and does work. However, your phone field may need to be casted to String or int depending on your user's phone instance field's type.
Related Topics
Android New Bottom Navigation Bar or Bottomnavigationview
Android Sdk Manager Won't Open
Don't Reload Application When Orientation Changes
How to Change the Text Color of Menu Item in Android
Android Listview Not Refreshing After Notifydatasetchanged
How to Set a Gradient Background in a Material Button from Material Components
Streaming Video from Android Camera to Server
Listadapter Not Updating Item in Recyclerview
Retrieve Incoming Call's Phone Number in Android
Capture Screen Shot of Googlemap Android API V2
Best Practice: Asynctask During Orientation Change
In Android, How to Set Margins in Dp Programmatically
Image in Canvas with Touch Events
How to Change Shape Color Dynamically
How to Programmatically Enable Auto Start and Floating Window Permissions
Get the Current Language in Device
Getting the Physical Screen Dimensions/Dpi/Pixel Density in Chrome on Android