简体   繁体   中英

Android Studio how to get data from the Firebase database?

I have this code and i wanna get the value String (how for example the names for users) from Firebase database in "Usuarios", which contains the users, to put it in the txtName variable.

public class HomeFragment extends Fragment {
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private Button btnLogOut;
private int CAMERA_REQUEST_CODE = 0;

private TextView textName;
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Usuarios");


public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.fragment_home, container, false);
    TextView textName = (TextView) view.findViewById(R.id.txtName);

    return view;
}

@Override
public void onPause() {

    super.onPause();
}

}

Thank you very much, if you could solve this and you would do a great favor :)

Firebase Database image

To display all the within those childrens, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usuariosRef = rootRef.child("Usuarios");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usuariosRef.addListenerForSingleValueEvent(eventListener);

The output will be:

Eric
Julian Ramos
//And so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM