import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.roedash.Models.Person;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class Profile extends AppCompatActivity {
    
    ImageView logo;
    Button logout;
    TextView id, fn, sn, email;
    DatabaseReference dbref;
    FirebaseUser student;
    List<String> itemList;
    String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        logo = findViewById(R.id.iv_Logo_Profile);
        logout = findViewById(R.id.btn_logout);
        student = FirebaseAuth.getInstance().getCurrentUser();
        uid = student.getUid();
        itemList = new ArrayList<>();

        id = findViewById(R.id.tv_Id_Profile);
        fn = findViewById(R.id.tv_fn_profile);
        sn = findViewById(R.id.tv_sn_Profile);
        email = findViewById(R.id.tv_email_profile);
        dbref = FirebaseDatabase.getInstance().getReference("_person_");
        dbref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                itemList.clear();
                String studentId = snapshot.child(uid).child("id").getValue(String.class);
                String sfName = snapshot.child(uid).child("fn").getValue(String.class);
                String sName = snapshot.child(uid).child("sn").getValue(String.class);
                String emailAddress = snapshot.child(uid).child("em").getValue(String.class);
                itemList.add(studentId);
                itemList.add(sfName);
                itemList.add(sName);
                itemList.add(emailAddress);
            }

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

            }
        });
        logout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(Profile.this, MainActivityLogin.class);
                startActivity(i);
            }
        });
        logo.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(Profile.this, Dashboard.class);
                startActivity(i);
            }
        });

    }
}

I am struggling with calling the user records from the Firebase DB, once the user is logged in or registered, the records should be displayed in the Profile activity. I have tried several different ways and none of them works, I guess I am missing something small but can not find what exactly. The user is successfully added while registering and the names of the database id's are correct. Please help!

This is from the console - class Person person -MVXdjNUHA-NfJ4oKLXJ em: fn: id: sn: -MVphfQK29jP5RelZViy em: fn: id: sn:

0

There are 0 best solutions below