firebase data change method in android studio - android

how email's value change from "app-user/users"?
Firebase state :
this isn't web language, i use javacode and xml of androidstudio.
i'm tryed under code.
databaseReference.child(app-user).child(user).orderByChild("email").equalTo("user4~~")
I don't know what to do next.

To get all users whose email address starts with user4:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("app-users/users");
Query usersQuery = usersRef.orderByChild("email").startAt("user4").endAt("user4\uF7FF");
usersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey()+": "+userSnapshot.getChild("displayName").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
To understand the usersRef.orderByChild("email").startAt("user4").endAt("user4\uF7FF") query, read it as:
Take all child nodes of usersRef and order them by their email property.
Then find the first child node that starts with user4.
Then return each child node, until you find a child that starts with .endAt("user4\uF7FF") (or further).
In this the \uF7FF is no magic code, but just the last known Unicode character. So by combining .startAt("user4").endAt("user4\uF7FF"), you're building a startsWith operator.

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

firebase get value from uid after unkown uid

CABARAN is an unknown Uid. it is not a text. Right now I have the uid, and I want to get value for the tajukPenuh.
This is the code and I still can't get the value.
FirebaseDatabase.getInstance().getReference().child("karangan").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Karangan karangan = child.child(karanganID).getValue(Karangan.class);
if (karangan != null) {
String tajukPenuh = karangan.getTajukPenuh();
holder.getTextViewKaranganID().setText("Karangan Tajuk: " + tajukPenuh);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You could in theory do a query like this:
FirebaseDatabase.getInstance().getReference().child("karangan")
.orderByChild("-LYgFIl4Xiv_Ls51Slvh/uid").equalTo("-LYgFIl4Xiv_Ls51Slvh")
.addListenerForSingleValueEvent(new ValueEventListener() {
But the problem is that you'd need a lot of indexes in your rules, which may be technically possible, but is unfeasible for most real usage.
Your current data structure makes it easy to find all the child nodes for CABARAN, but it does not make it easy to find CABARAN for a given child node. To allow that use-case to run efficiently, you should expand your data structure with a so-called reverse index that maps back to CABARAN from the value that you know. So something like:
"myIndex": {
"-LYgFIl4Xiv_Ls51Slvh": "CABARAN",
"-LzfFIl4Xasas51Slads": "CABARAN",
"-Lasddas981398asdh1h": "CASITWO"
}
This is an additional data structure, that you will have to keep up to date when you're writing the rest of the data. But with this structure, it now becomes very easy to determine that -LYgFIl4Xiv_Ls51Slvh maps to CABARAN.
For more on this, see my answer here: Firebase query if child of child contains a value

Android Studio firebase get key of child with specific value

I have am developing an app in Android Studio and I am using FireBase to store data. Lets say I have a node that has multiple children, I would like to get the key of the child that has a specific value. For example;
I would like to read from the node 'languages', the key of the child with value 'English' so that I get 'eng'. Note that upfront I do not know that the key is 'eng'!
Someone knows a way to achieve this, without importing all the children?
Try something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("languages");
ref.orderByValue().equalTo("English").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
String value = snap.getValue(String.class);
String key = snap.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How to perform search in a FirebaseDatabase?

Description
My Firebase NoSQL Database should look somewhat like this:
A user Java model should get pushed whenever a new user signs in.
Problem
How to check if the user with the email address already has an account? Since the email address is at: Root > PushID > email; and push ID is automatically generated, I am not sure on how to iterate on all push ID's and check if any of them has its email key set to the specified value.
I have read the documentation but unable to figure out how to solve this problem.
Learning from Shubhank's answer
Firebase Methods which involve a child can be applied to any of its children irrespective of its level in the hierarchy i.e. not limited to immediate children.
In this problem, the child to be searched was 2 levels below the common parent. The first level child can be ignored and query methods can be directly applied on the target child.
You should first make a model class for the user
class User {
public String email;
public String name;
}
Then get the users from the db and map them into User objects
Simple loop through all results query
FirebaseDatabase database = FirebaseDatabase.getInstance();
// i don't know the end point since you have not specified it in the image
database.getReference("myUsersEndPoint").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
User user = data.getValue(User.class);
if (myField.getText().toString().eqaulsIgnoreCase(user.email)) {
// exist
}
}
}
#Override
public void onCancelled(DatabaseError databaseError)
Log.w("MyApp", "getUser:onCancelled", databaseError.toException());
}
});
Searching only for specific Values
In this example, we use the orderByChild and equalTo method to limit result to specific child value
FirebaseDatabase database = FirebaseDatabase.getInstance();
// i don't know the end point since you have not specified it in the image
database.getReference("myUsersEndPoint").orderByChild("email").equalTo("emailToSearchhere").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
// here the user will have the specified email only
}
}
#Override
public void onCancelled(DatabaseError databaseError)
Log.w("MyApp", "getUser:onCancelled", databaseError.toException());
}
});

Categories

Resources