Query Specific Child Form Firebase - android

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) {
}
});

Related

is there best way to read firedatabase for recycleview in multiple child nodes?

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

fetching related data using the same uid

i trying to read related data from dataorder which related using userID from datauser.
private void cobacoba(){
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
DatabaseReference databaseUser = FirebaseDatabase.getInstance().getReference("datauser");
databaseUser.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String nama = dataSnapshot.getValue(dataUser.class).getUserId();
hey.setText("haloo "+nama);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
i use code above to read data from datauser but how do i read the related data on dataorder ?
i try the same way to read the idOrder from dataorder but the value always null. can some one help please ?
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
final String userid = user.getUid();
DatabaseReference databaseOrder = FirebaseDatabase.getInstance().getReference("dataorder");
databaseOrder.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final String idorder = dataSnapshot.getValue(dataOrder.class).getIdOrder();
if (!TextUtils.isEmpty(idorder)){
status.setText(idorder);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
final String userid = user.getUid();
final DatabaseReference databaseOrder = FirebaseDatabase.getInstance().getReference("dataorder");
databaseOrder.child(userid).child(idorder).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String haritanggal = dataSnapshot.getValue(dataOrder.class).getHaritanggalOrder();
String jamlapangan = dataSnapshot.getValue(dataOrder.class).getLapanganjamOrder();
idordeer.setText("Order ID = "+idorder);
jamtanggal.setText("Tanggal dan Waktu = "+jamlapangan+" Tanggal "+haritanggal);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}else {
status.setText("no current order");
}
status.setText("id anda= "+idorder+" ....."); //<<<id value always null, why ?????
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
using code above, i discover idOrder always null thats why i cannot access the data inside it, because i don't have the key. any sugestion please ?
Please check your query :
databaseUser.child(userid).child(idorder)
instead use :
databaseOrder.child(userid).child(idorder)

Read child value from firebase

i want to get only one data from firebase .made lot of efforts but could not be successful
the structure of the data of my firebase is as follows
and trying something like this in my code
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue(String.class);
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});
If you want to get only the email you can do something like this :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue().toString();
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});
If you dont want to load the other data, you can directly query the email like this :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("users").child("userID").child("email");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue().toString();
//do what you want with the email
setDisplayName.setText(email);
}
#Override
public void onCancelled(DatabaseError databaseError) {
setDisplayName.setText("data not get" + databaseError );
}
});

How to get specific child data from firebase

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..

How to fetch the username list in listview in Android from Firebase database?

I have registered and saved users through my app and now I want to fetch the list of usernames as shown below from Firebase database. How do I do it in list view? my database is shown below:
ArrayList<MODELCLASSNAME> Users=new ArrayList<>();
DatabaseReference df=FirebaseDatabase.getInstance().getReference().child("users");
df.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapShot user : dataSnapshot.getchildren()){
Users.add(dataSnapshot.getValue(MODELCLASSNAME.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Now loop through your arraylist
get your database reference
firebaseDatabase = FirebaseDatabase.getInstance();
mPostDb = firebaseDatabase.getReference("User");
Now query your table
Query query = mPostDb.orderByKey();
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapShot user : dataSnapshot){
// Do what ever you whatever you want with this user object
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try this method:
public void retrieveNames() {
Query myQuery = mDatabase.orderByChild("name").equalTo("INSERT-HERE-A-NAME-FOR-YOUR-SEARCH");
myQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Iterator iterator = dataSnapshot.getChildren().iterator();
//FOR YOUR DATA STRUCTURE -
while (iterator.hasNext()) {
String image_uri = (String) ((DataSnapshot) iterator.next()).getValue();
String name = (String) ((DataSnapshot) iterator.next()).getValue();
String username = (String) ((DataSnapshot) iterator.next()).getValue();
YourUserObject User = new YourUserObject (image_uri, name, username);
YourArrayList.add(User);
}
YourAdapter.notifyDataSetChanged();
}
#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) {
}
});
}

Categories

Resources