firebase get value from uid after unkown uid - android

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

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.

What would be the best way of getting the child "info" without the parent key?

I want to get only the "info" here. what would be the best way of getting so?
The Firebase Realtime Database always returns full nodes in its result. There is no way to only read the Info child of each node in this data structure. If you want that, you should store the Info for each pair in a separate top-level node, like:
Breedings
Name+Name
...
NameES+NameEE
...
Infos
Name+Name
assigningDate: ...
fatherPGN: ...
motherPGN: ...
NameES+NameEE
assigningDate: ...
fatherPGN: ...
motherPGN: ...
With your current structure the best you can do is to read all data, and only process the Info for each child node. To do that you'd use something like:
DatabaseReference pairsRef = FirebaseDatabase.getInstance().getReference("Pairs");
pairsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot pairSnapshot: dataSnapshot.getChildren()) {
DataSnapshot infoSnapshot = pairSnapshot.child("Info");
Log.i("PAIRS", pairSnapshot.getKey());
Log.i("PAIRS", infoSnapshot.child("assigningDate").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
Note that there have been quite a few questions about this topic before, so I recommend looking at those too.

firebase data change method in android studio

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.

How to retrieve child data under 2 random keys

How do I display the RequestsID on a recyclerview only when stallID = 1?
How should my DatabaseReference be?
update:
Your database structure would not support orderByChild() from top node. So you would have to set your database reference upto the node food and then you can use orderByChild() query to find the Childs with certain stallID.
This, in code would look something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests").child("1542..").child("foods");
ref.orderByChild("stallID").equalTo(stallIDYouWant).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
// do here what you wanted
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// ToDo: don't ignore this, do something for errors
}
)};
This would in fact not help you much because you'd have to set this for every unique id in your database structure.
So the better thing to do here would be make your database structure different. To know more how to make your structure good for this operation, refer this answer.

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