I have need to access a specific child from node, i have only "available key" of the specific child not complete path.
Since you know the key of the child you're looking for, you can query on that:
DatabaseReference schoolsRef = FirebaseDatabase.getInstance().getReference().child("School");
Query query = schoolsRef.orderByKey().equalTo("MZ3bW5kLJAQorgnZbYiTaOoWWSG2");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
String email = userSnapshot.child("email").getValue(String.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
Note that you can't use orderByChild("id") as explained in my answer
here: Firebase Query Double Nested.
Just add this code:
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("Email").getValue().toString();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
try this :-
DatabaseReference mUserNameReference = FirebaseDatabase.getInstance().getReference("School").child("id").child("id");
mUserNameReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue().toString());
//get all the key value like this
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
If you have key for that child but not complete path first get key of parent node in list like below I did.
Arraylist<String> keyList =new Arraylist();
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(firebaseUser.getUid();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
String value= childSnapshot.getValue(String.class);
keyList.add(key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
now use for loop or foreach loop to check if that node contains available child key.
for(String key:keyList){
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("school").child(key).orderByChild("id").equal(availableKey);
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
Object object= childSnapshot.getValue(Object.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
hope this will help you..
Related
Hi i have this database: farmerUser, user, and livelihood.
Each nodes contains information in separate nodes.
For example: i want a farmer with this specific job and gets the contact information in user node and get its job description in this another node.
This is my code. This code is working, but i wonder if there is a best way to read multiple nodes to store it to my recycleview.
investingRecycleView.setLayoutManager(new LinearLayoutManager(this.getContext()));
FirebaseDatabase.getInstance().getReference().child("farmerUser").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot1 : dataSnapshot.getChildren()) {
String id = postSnapshot1.getKey();
String farmMethod = postSnapshot1.child("farmingMethod").getValue().toString();
FirebaseDatabase.getInstance().getReference().child("ventureHeader").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot4) {
for (DataSnapshot postSnapshot3 : snapshot4.getChildren()) {
String expectedReturn = postSnapshot3.child("expectedReturn").getValue().toString();
FirebaseDatabase.getInstance().getReference().child("livelihood").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
for (DataSnapshot postSnapshot2 : snapshot1.getChildren()) {
String budget = postSnapshot2.child("budget").getValue().toString();
String farmTag = postSnapshot2.child("farmingTag").getValue().toString();
FirebaseDatabase.getInstance().getReference().child("user").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot3) {
for (DataSnapshot postSnapshot3 : snapshot3.getChildren()) {
String name = postSnapshot3.child("firstName").getValue().toString();
String lastName = postSnapshot3.child("lastName").getValue().toString();
String phoneNum = postSnapshot3.child("phoneNum").getValue().toString();
FarmerVentureListModel farmerVentureListModel = new FarmerVentureListModel("Name:"+name + " " + lastName, "Farming Method:"+farmMethod,"#"+ phoneNum, "Farming tags:"+farmTag, "Budget:"+budget,"Expected return:"+expectedReturn);
list.add(farmerVentureListModel);
investingRecycleView.setAdapter(farmerVentureInvestingRow);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I wonder if there is a way like join each nodes without reading the entire data to find the match
I am trying to read data from firebase in my android app. This is how it looks.
I want to read the marked data. I am able to read the key of categories child. But unable to read the ids values.
Here's my code:
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
String name = ds.getKey();
String ids = ds.child("ids").getValue(String.class);
Log.i(name, "onDataChange: "+ids);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You could not connect to the root you want. For example, tcat2, you can modify the ids.
You must specify the path from which you want to read.
You can try this :
dbCategories = FirebaseDatabase.getInstance().getReference("categories");
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
String name = ds.getKey();
for(DataSnapshot ds1 : ds.child(name).child("ids").getChildren()){
String ids = ds1.getValue(String.class);
Log.i(name, "onDataChange: "+ids);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I hope I helped you.
I have the structure in the image below and I want to get the whole data from ALL NODES querying by "amount". How can I do that query? How can I get amount value from all the nodes because all roots are different.
I treid:
zonesRef= FirebaseDatabase.getInstance().getReference("Database");
zone1Ref = zonesRef.child("Payments");
Query queryRef = zone1Ref.orderByChild("amount");
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
I have differnt users with different UserID. Here is code which i am usign to push data to firebase.
currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
UserID= currentFirebaseUser.getUid().toString();
zonesRef= FirebaseDatabase.getInstance().getReference("Database");
zone1Ref = zonesRef.child(Type);
zone1NameRef = zone1Ref.child(UserID);
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Amount2= Amount.getText().toString();
String Description2= Description.getText().toString().trim();
String uniqueKey = zone1NameRef.push().getKey();
UniqueKey= zone1NameRef.child(uniqueKey);
UniqueKey.child("amount").setValue(Amount2);
UniqueKey.child("date").setValue(currentDateTimeString);
UniqueKey.child("description").setValue(Description2);
UniqueKey.child("school").setValue(School);
}
});
Upon checking your data structure you have two keys before accessing the amount.
DatabaseReference getKeys;
DatabaseReference Amounts;
getKeys = FirebaseDatabase.getInstance().getReference("Database");
Amounts = FirebaseDatabase.getInstance().getReference("Database");
getKeys.child("Payments").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
String key = snapshot.getKey();
Amounts.child("Payments").child(key).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot1:dataSnapshot.getChildren()) {
String amount = snapshot1.child("amount").getValue(String.class);
Toast.makeText(getApplicationContext(),amount,Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I am an absolute beginner in Android and Firebase. Forgive me for my post. I want to get the key element that is under "node" eventos. Till I have done the following code, but I am no able to get the key. Please help me, I am doing my college project.
DatabaseReference mDataBase = `FirebaseDatabase.getInstance().getReference();`
DatabaseReference usuarios = mDataBase.child("usuarios");
DatabaseReference eventos = usuarios.child("eventos");
eventos.orderByValue().equalTo("eventos").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
String key = snap.getKey();
System.out.println("LAS CLAVES SON: "+key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
enter code here
}
});
I want to get the key to update each element and delete.
you have to just remove your some code :-
eventos.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
String key = snap.getKey();
System.out.println("LAS CLAVES SON: "+key);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
enter code here
}
});
I got 2 same methods in one I declare string in Query and on another I declare it thru variable, but when I run apk the one I declare string inside Query works but the other does not.
First method:
private void randomtest() {
Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("username").equalTo("darkarcher5");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Second method:
private void neRandomTest() {
if (username != null) {
Query query = FirebaseDatabase.getInstance().getReference("user").orderByChild("username").equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Error given by second method: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
In your first method, your query is referencing on the "users" node while on your second method your query is referencing on the "user" node. If I am correct you are trying to connect to "user" node in second method which is not available in the firebase database. That's why indexoutofbounds.
Try using this:
private void neRandomTest() {
if (username != null) {
//Query query = FirebaseDatabase.getInstance().getReference("user").orderByChild("username").equalTo(username);
Query query = FirebaseDatabase.getInstance().getReference("users").orderByChild("username").equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
User user = users.getValue(User.class);
Log.e("username", user.getUsername());
Log.e("password", user.getPassword());
userList.add(user);
}
txtViewPass.setText(userList.get(0).getUsername());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}