简体   繁体   中英

How to get data from a child in Firebase Database?

I am struggling to get data from a child in my Database to settext in a TextView .

Here is my Database: Database

I think the problem is that I don't have the Uid right after Ansatte but the name instead. If I take away name , and just have Ansatte ,Uid, and the rest of the data it works.

Here is my code for getting data:

 databaseReference = FirebaseDatabase.getInstance("").getReference("Ansatte");
    btnadd = (findViewById(R.id.btn_leggtilansatt));

    mAuth = FirebaseAuth.getInstance();
    currentid = mAuth.getCurrentUser().getUid();




    tvtest = findViewById(R.id.tvtest);

    databaseReference.child(currentid).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            if (snapshot.exists()){
                String user = snapshot.child("name").getValue().toString();

                tvtest.setText(user);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

And here is the code for storing:

private void add() {
    String name = editText1.getText().toString().trim();
    String email = editText2.getText().toString().trim();
    String storage = editText3.getText().toString().trim();
    String password = tv.getText().toString().trim();

    if (!TextUtils.isEmpty(name)){





        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()){

                    Ansatt ansatt = new Ansatt (name, email, storage);

                    databaseAnsatt.child(name).child(mAuth.getUid()).setValue(ansatt);

                    Toast.makeText(getApplicationContext(), "Hei" , Toast.LENGTH_LONG).toString().trim();
                    Intent intent = new Intent(AddEmployeeActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            }
        });

    }

To be able to get the value of the name field that exists under the following location:

root/Ansatte/Name/$uid

Please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = db.child("Ansatte").child("Name").child(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            String name = snapshot.child("name").getValue(String.class);
            tvtest.setText(name);
            Log.d("TAG", name);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be:

name

And this value will be also set to your tvtest TextView.


PS Don't forget to have set the proper security rules.

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