title: How do I relate new data to a currently logged in user in Firebase?
body: <p>I want to add a student list to my Firebase realtime database. When a teacher adds a new student, I want the currently logged-in teachers ID to relate to the student they just added.</p>
  <p>When I compile the following I get my &quot;Student Added&quot; toast on the emulator but nothing shows up on Firebase - no new parent of &quot;students&quot; and no new entries. Can someone see where I'm going wrong?</p>
  <p>I think using datasnapshot to get thecurrentUserId from Firebase might be long-winded but when I just used the simpler .getCurrentUser().getUid() I was getting the same result, successful toast but nothing showing in the Firebase database.</p>
  <pre><code>package com.example.dissertation814.teacher.studentlist;
  
  import java.util.Objects;
  
  public class AddStudentActivity extends AppCompatActivity {
  
  //firebase auth
  private FirebaseAuth mAuth;
  
  //navigation variables
  public String currentUserAccount;
  public String teacherAccountNav = &quot;Teacher&quot;;
  
  //user id from firebase
  public String currentUserId;
  
  //add student variables
  EditText editTextName;
  EditText editTextEmail;
  Button buttonAddStudent;
  
  DatabaseReference databaseStudents;
  
  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add_student);
  
      //initialise views
      editTextName = findViewById(R.id.nameEditText);
      editTextEmail = findViewById(R.id.emailEditText);
      buttonAddStudent = findViewById(R.id.addStudentButton);
  
      //get current user id from firebase
      mAuth = FirebaseAuth.getInstance();
      String uid = Objects.requireNonNull(mAuth.getCurrentUser().getUid());
      DatabaseReference userIdDetails = FirebaseDatabase.getInstance().getReference().child(&quot;users&quot;).child(uid);
      userIdDetails.keepSynced(true);
  
      //addlistener for current user id
      userIdDetails.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              currentUserId = dataSnapshot.child(&quot;userId&quot;).getValue().toString();
              //creates new node 'students' linked to 'userId' of current user
              databaseStudents = FirebaseDatabase.getInstance().getReference(&quot;students&quot;).child(currentUserId);
          }
  
          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
  
          }
      });
  
      //calls addStudent method when clicked
      buttonAddStudent.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              addStudent();
          }
      });
  }
  
  //add new student method
  private void addStudent(){
      String studentName = editTextName.getText().toString().trim();
      String studentEmail = editTextEmail.getText().toString().trim();
  
      if(!TextUtils.isEmpty(studentName)){
          //this string is getting the id from firebase
          String id = databaseStudents.push().getKey();
  
          Student student = new Student(id, studentName, studentEmail);
  
          //this sets the student at the child from the id
          //use this to set student at the child for the current user
          databaseStudents.child(id).setValue(student);
  
          Toast.makeText(this, &quot;Student Added&quot;, Toast.LENGTH_LONG).show();
  
      }else{
          Toast.makeText(this, &quot;Name required&quot;, Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>