Displaying value from child - android

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

Related

how to read all child nodes values and use them in firebase android

This is my Database in Firebase
I want to get the values of Students under Courses
and then I want to delete the course name under courses in users(that username match the values I got from Courses/Students )
after clicking on a button
This is my code
DatabaseReference mCourseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Courses").child(courseModel.getCourseName());
final DatabaseReference mStudentDB = FirebaseDatabase.getInstance().getReference().child("Users");
mCourseDatabaseReference.child("Students").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(final DataSnapshot snapshot1 : dataSnapshot.getChildren()){
final User student = snapshot1.getValue(User.class);
mStudentDB.orderByChild("userName").equalTo(student.getUserName()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren())
{
String stuserkey = data.getKey();
mStudentDB.child(stuserkey).child("Courses").child(courseModel.getCourseName()).removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { }});
but it doesn't work.
can you help me, please?
thanks.

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

How can I get some value from my Firebase?

I am trying to solve how to get a value from my DatabaseReference, but doing so gives me the full address when I want to put it in a TextView
This is the reference:
public static DatabaseReference getUsernameRef(String email){
return FirebaseDatabase.getInstance().getReference("nom_usuarios");
These are the values that I want to rescue:
i tried this:
mPosti.setUsername(FirebaseUtils.getUsernameRef(FirebaseUtils.getCurrentUser().getEmail()).toString());
But instead of receiving the value, I get the address of the database.
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("nom_usuaios").getRef();
database.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// here you will the value in datasnapshot
for (DataSnapshot dataSnapshot : dataSnapshot.getChildren()) {
list.add(dataSnapshot.getValue());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How I Will Apply Query In Firebase By Checking Two Childs

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

retrive data from firebase

I'm new to android developing. I tried retrieving data from firebase. which worked . but I'm unable to filter unique identifier from database results .
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("foods",dataSnapshot.getValue().toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
results are like
XsgGhY7qUY5Qa3t8 = {food = apple} , ..... etc
I want to remove that unique identifier & just display results as
Apple , Mango ...etc
thank you
I figured the answer. If anyone has same issue use "child event listener "
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("foods",dataSnapshot.getValue().toString());
}
#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) {
}
});
Try this:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
Log.d('foods',dataSnapshot.child('food').getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adding child() to your reference you can access an specific key in your tree. But as Frank suggests try using a POJO class to retrieve all your data from Firebase
edited: sorry i forgot you had many children, you need to make for (DataSnapshot snap: dataSnapshot.getChildren()) {//code} when your have many children in your node

Categories

Resources