0

我想将学生列表添加到我的 Firebase 实时数据库。当老师添加新学生时,我希望当前登录的老师 ID 与他们刚刚添加的学生相关。

当我编译以下内容时,我在模拟器上得到了我的“学生添加”吐司,但 Firebase 上没有显示任何内容 - 没有“学生”的新父级,也没有新条目。有人能看出我哪里出错了吗?

我认为使用数据快照从 Firebase 获取 currentUserId 可能是冗长的,但是当我只使用更简单的 .getCurrentUser().getUid() 时,我得到了相同的结果,成功的 toast 但在 Firebase 数据库中没有显示任何内容。

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();
    }
}
4

0 回答 0