firebase orderByChild() with specified child key doesnt work - android

I'm trying to get data which has current user uid and sort by time, but somehow my query brings data which has other uid as well
This is my query
FirebaseDatabase.getInstance().getReference().child("posts").orderByChild("uid_timestamp/"+uid).limitToLast(12)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
items.clear();
for (DataSnapshot item : dataSnapshot.getChildren()) {
items.add(item.getValue(PostModel.class));
Log.d("checking","writerUid "+item.getValue(PostModel.class).writerUid);
}
This is my database
So the writerUid is in uid_timestamp with the time. Am I using wrong child key? I need your help!

Related

How to get values from different child in firebase realtime database?

Here is my Database architecture where Jobs is the main child and under that there will be users child, and an user can post multiple time. now i want to get all the posts done by all the users at once. is it possible to do that?
https://i.stack.imgur.com/DT3MO.jpg
setQuery(FirebaseDatabase.getInstance().getReference().child("Jobs")
Used that query to get all of the posts from all of the users but didnt work. any better solution for this problem?
setQuery(FirebaseDatabase.getInstance().getReference().child("Jobs").child("Uid");
this one works but cant use this one because i want to set free for all users to read data.
You can do the following:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Jobs");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
for (DataSnapshot childSnapshot: ds.getChildren()) {
// get the child attribute under the random key
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
Since you have a reference to node Jobs then in the first iteration you can get the random keys (highlighted in your image), and in the second iteration you can get the attributes inside those keys.

Android & Firebase : How can i get data from frebase by key

Firebase database structure
Please how i can get data from firebase with IMEI of the phone.
How i can get listcarte by the phone TPEID!!
If the value you're looking for is always in 0/TPEID, then you can use a query to find the nodes that contains that value with:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(...);
Query query = ref.orderByChild("0/TPEID").equalTo("869688030960688");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot tpeSnapshot: dataSnapshot.getChildren()) {
System.out.println(tpeSnapshot.getKey()); // Tpe0
System.out.println(tpeSnapshot.child("0/TPEID").getValue(String.class)); // 869688030960688
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
If the value is not at a fixed path under each child, you won't be able to search for it with a Firebase query. In that case, see Firebase Query Double Nested, firebase get url by nested child id for tree like, Firebase query if child of child contains a value

want to conduct firebase android nested query

I want to be able to query the tags from each category eg, Shirts, Pants , etc. In my firebase console i only have Shirts for now.
In my app recouture i have currently 2 users using and a Shirts node. Ideally there would be other categories added to it as well. Eg, Pants, Shoes
I would like for each user to conduct a query on the tags under each category and each would have a similar structure to Shirts.
Hope my question was clear !
Try the following:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Shirts");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
DataSnapshot tags=ds.child("tags");
for(DataSnapshot datas: tags.getChildren()){
String tagValues=datas.getValue().toString();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here you snapshot is at Shirts then you loop and to be able to access the child tags, then you loop inside the node tags and retrieve the values Style and Tags.

android firebase database: how to get all nodes that are in a set of values

I am trying to retrieve a list of friends for a user so I can display them in a list view. The friends and user info is structured like this in my firebase database:
So basically, I want to take the user ids listed in the friends part and query my users data to get all the info under every node that is in that set of user ids. How can I achieve this using the android firebase database sdk querying? I would like to be able to retrieve all the users in a single database query.
Thanks.
You cannot get that data in single query, you need to query your database twice. This is a common practice when it comes to Firebase. Assuming that friends and users nodes are direct childs of your Firebase root, to achieve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendIdRef = rootRef.child("friends").child(friendId)
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference usersRef = rootRef.child("users").child(key);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String username = dSnapshot.child("username").getValue(String.class);
Log.d("TAG", username);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
friendIdRef.addListenerForSingleValueEvent(valueEventListener);
It will print all user names of those particular users. One more thing to note, is that you don't need to add in your database those friends that have the value of false, only those with the value of true.

How to get all child's data in firebase database?

I have this firebase database
and i need to get all phone numbers of users , which listener shall i use to get all childes?
Every user is added as an object with user-ID as a name of that object, I need to retrieve this objects without knowing that user-ID
I searched the documentation , it's related to DataSnapshot but i couldn't get a DataSnapshot without a listener ! is it correct to seek a DataSnapshout or shall i use something else
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.
DatabaseReference ref1= FirebaseDatabase.getInstance().getReference();
DatabaseReference ref2,ref3,ref4;
ref2 = ref1.child("User");
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Userlist = new ArrayList<String>();
// Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
Userlist.add(String.valueOf(dsp.geValue())); //add result into array list
}
/* userlist will store all values of users, then point to every userlist item
and get mobile numbers*/

Categories

Resources