How do I relate new data to a currently logged in user in Firebase?

橙三吉。 提交于 2020-07-31 04:19:47

问题


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.

When I compile the following I get my "Student Added" toast on the emulator but nothing shows up on Firebase - no new parent of "students" and no new entries. Can someone see where I'm going wrong?

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.

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 = "Teacher";

//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("users").child(uid);
    userIdDetails.keepSynced(true);

    //addlistener for current user id
    userIdDetails.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            currentUserId = dataSnapshot.child("userId").getValue().toString();
            //creates new node 'students' linked to 'userId' of current user
            databaseStudents = FirebaseDatabase.getInstance().getReference("students").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, "Student Added", Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(this, "Name required", Toast.LENGTH_LONG).show();
    }
}

来源:https://stackoverflow.com/questions/62814670/how-do-i-relate-new-data-to-a-currently-logged-in-user-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!