How can I call information from Firebase Database to Android Studio and show it in an activity?

0

Good day, I have this information saved in Firebase Database

I would like to read it and show it in an activity in the following way.

I have this code, I do not know what would be the simplest option or how would the onDateChange method be used?

Thank you.

    
asked by Julian 26.09.2018 в 17:28
source

1 answer

0

first you have to add your project to firebase, then import the corresponding libraries in this case would be the FireBaseDatabase libraries, now if we go to the Code, you can do it in two ways

fReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
   //si hay cambios realizados
    String value = dataSnapshot.getValue(String.class);
    Log.d(TAG, "Value is: " + value);
}

@Override
public void onCancelled(DatabaseError error) {
    // Failed to read value
    Log.w(TAG, "Failed to read value.", error.toException());
}});

You can also do it with the ChildListener event

ChildEventListener listener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

          String value = dataSnapshot.getValue(String.class);//si se agrego un nuevo hijo los valores de ese hijo estaran en la variable String value
        }

    }

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

    }
};

and in your Oncreate

fReference.addChildEventListener(listener);

the add function shows you all the ones that are added in real time, the changed function shows you immediately the changes made in real time the move if the file was moved or the data is removed because if they were deleted

I hope I have helped you;) regards

    
answered by 26.09.2018 в 17:47