How can I get some value from my Firebase? - android

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

Related

read data from firebase and set the value of textview

i faced an issue like user entered enrollment number and verify and read all data from that and set the value to text view
If user enter enrollment 66 then verify that enrollment and read all that data from 66 and set value of text view to marks of subjects.what i need to do if i want to solve Above problem?
I tried:
reff= FirebaseDatabase.getInstance().getReference().child("Marks");
reff.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String mm=dataSnapshot.child("mcad").getValue().toString();
String jm=dataSnapshot.child("java").getValue().toString();
String nm=dataSnapshot.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
First, your Database Reference returns you a list of data so you need to receive your data as a list.
Second, if you want to filter your data base on enrollment then add a query in your database reference.
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query query = databaseReference.child("Marks").orderByChild("enrollment").equalTo(66);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
String mm=data.child("mcad").getValue().toString();
String jm=data.child("java").getValue().toString();
String nm=data.child("nma").getValue().toString();
txtmcadmarks.setText(nm);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

how to get URI of an image from Datasnapshot

I am making an app where I can upload images to firebase then retrieve them and view them in a viewpager.
The images have a name and an url.
In addition to that, I want to be able to write the name of the image in a textbox and display the corresponding image in an imageview.
This is the code I'm working with :
private void findImage(){
final Query query = mDatabaseRef.orderByChild("name").equalTo(EditTextName.getText().toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot NamedImage : dataSnapshot.getChildren()) {
//i have tried this first but i don't know what method i should use to get the URI from the datasnapshot.
image.setImageURI(NamedImage.getValue());
//then i have tried this but i can't use "context" here, nor can i apply .getImageUrl to the datasnapshot.
Glide.with(context).load(NamedImage.getImageUrl()).into(image);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I am new to android studio and java and I haven't studied it before starting this application.
Any idea on what I should do please?
I found the solution to my problem :
private void findImage(){
final Query query = mDatabaseRef.orderByChild("name").equalTo(EditTextName.getText().toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot NamedImage : dataSnapshot.getChildren()) {
SliderUtils sliderUtils = NamedImage.getValue(SliderUtils.class);
Glide.with(getApplicationContext()).load(sliderUtils.getImageUrl()).into(image);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

How can i get Multiple values of multiple nodes using Datasnapshot from the Firebase Database?

I want to display dayVisitors of all the dates. Hence i want to access all the dayVisitors under Noida Sec1/ all dates.Database structure
This is what I've done but it gives null pointer error: Attempt to invoke virtual method 'java.lang.String com.example.thehighbrow.visitormanagement.DayVisitor.getName()' on a null object reference.
If you want to specifically access just the dayVisitors under Noida Sec 1 you can simply implement this using:
final FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference ref = db.getReference("Noida Sec1");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
if(childSnapshot.hasChild("dayVisitor")) {
for (DataSnapshot visitorSnapshot : childSnapshot.child("dayVisitor").getChildren()) {
Visitor visitorObject = visitorSnapshot.getValue(Visitor.class); //or whatever your dayVisitor object is
//now you can access visitorObject with the fields you created and do whatever like add it to an arraylist
}
}
}
} #Override
public void onCancelled(DatabaseError databaseError) {
Log.e("READ FAILED", databaseError.getMessage());
}
});
I'm not sure what your code is doing, however, I do suggest, if possible, you try to format your database layout to be as flat as possible because nesting data in this manner can get very messy and inefficient. Maybe make dayVisitor a field in visitor rather than its own child node.
First of all get the reference to the children of Noida Sec 1 like:
DatabaseReference mNoidaReference = mFirebaseDatabase.getReference().child("Noida Sec1");
Now make a childEventListener for it and loop through it to find the dayVisitor child
ChildEventListener mChildEventListener = new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for(Datasnapshot data: dataSnapshot.child("dayVisitor")){
String dayVisitor = data.getValue();
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
mNoidaReference.addChildEventListener(mChildEventListener);

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

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