How I Will Apply Query In Firebase By Checking Two Childs - android

I want to apply query in firebase as
"Those Students Who Are Having More Marks Then 70 in tenthP as well as in twelveP" and then show list of their names in list view.
This is I am using query in one child :
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("users").orderByChild("tenth").startAt("70").endAt("100");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(FilterStudentsActivity.this,"Yes Exist",Toast.LENGTH_LONG).show();
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot issue : dataSnapshot.getChildren()) {
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Related

How delete the particular child of child in firebase realtime database?

I want to delete the particular child of a child in firebase realtime database.
I have added a picture of my database.
I want to remove the highlighted child.
You can try this code.
Query query = myRef.child("Purchase_Request").orderByChild("Chimney_Purchase");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
if(snap.getKey().equals("your_key")) {
snap.getRef().removeValue();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});

Firebase join using Android Studio

I need to do join operation for data using the unique key provided by the Firebase database.Table habitat is created under the unique key of snake table.And also need to retrive data for list view or recyclerview.
databaseSnake = FirebaseDatabase.getInstance().getReference()
.child("snake")
.orderByKey()
.addValueEventListener(
new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot child : dataSnapshot.getChildren())
{
// HERE CORRESPONDS TO JOIN
DatabaseReference Databasesnake = FirebaseDatabase.getInstance().getReference()
.child("habitat")
.orderByKey()
.addValueEventListener(
new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
// repeat!!
}
#Override
public void onCancelled(DatabaseError databaseError)
{
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError)
{
}

I'm using firebase database, I just wanna know if can I or how can I retrieve data inside of two sub childs?

This is the structure of my database:
I want to get all the outcome value, but I need to loop the faculty child first and the exam child before I able to use the getValue() in outcome. How can I loop the faculty child and exam child in the same time?
This is my code, but I don't know how to get to the Exam child and loop it again to get the outcome value.
dbOutcome.child("sample_list").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You can use the models classes to retrieve this.
Make model classes and get data in them
public class Exam {
pirvate String outcomes;
// create constructor and getter setter
}
create Faculty class
public class Faculty {
List<Exam> exams
// create constructor and getter setter
}
get Data from snapshot in onDataChange like this
public void onDataChange(DataSnapshot dataSnapshot) {
List<Faculty> faculties = (ArrayList<Faculty>) dataSnapshot.getValue();
}
I want to get all the outcome value
Assuming that the sample_list node is a direct child of your Firebase root, to solve this, you need use getChildren() method twice:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("sample_list");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String outcome = ds.child("outcome").getValue(String.class);
Log.d("TAG", outcome);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
pass
fail
pass
fail

How can I reach second child in Android firebase

I want to reach second child directly , first child name is random.
I want to reach kullaniciadi child;
https://i.hizliresim.com/dO58mp.png
try this:
DatabaseReference db=FirebaseDatabase.getInstance().getReference().child("Kullanicilar");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
String kullaniciad=data.child("kullaniciadi").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here you have the datasnapshot at child Kullanicilar. Then you iterate inside of it and get the child kullaniciadi that is under the pushids

Displaying value from child

what is the query to display specific database, "nama" from database, but got all value in child, i used this code
dbResepNusantara.orderByKey().addValueEventListener(new ValueEventListener(){...
Database structure
value
FirebaseDatabase database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref= database.child("resepNusantara");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren()){
String value=data.child("nama").getValue().toString();
Log.i("nama",value);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The above will give you value of child nama.
If you add a breakpoint, this line public void onDataChange(DataSnapshot dataSnapshot) { will give you all the keys and values under resepNusantara, since dataSnapshot is on that location.
If you only want it to give you only nama from child(1) then you have to be more specific of the location and do this:
DatabaseReference ref= database.child("resepNusantara").child("1").child("nama");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) { //dataSnapshot location is child("nama")
String value=dataSnapshot.getValue().toString();
Log.i("nama",value);
}

Categories

Resources