多重更新Firebase数据库Android

时间:2017-06-25 19:26:04

标签: android firebase firebase-realtime-database firebase-authentication qr-code

我正在创建一个应用程序,学生将使用该应用程序扫描由教师创建的类的QR代码。

学生扫描后,她/他的详细信息将存储在班级列表数据中。我的问题是当我尝试更改名字,中间名和姓氏时,只有第一个框正在更新,而第二个和第三个框则没有。

The first oblong is the user ID of the teacher and the second one is the value of the QR code for the class he/she created.

有人可以帮帮我吗?

以下是更新第一个框的代码:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child("student").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String uid = user.getUid();
    String newFirstName = oldEmail.getText().toString().trim();
    String newMiddleName = middlename.getText().toString().trim();
    String newLastName = lastname.getText().toString().trim();
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        ref.child("users").child("student").child(uid).child("firstname").setValue(newFirstName);
        ref.child("users").child("student").child(uid).child("middlename").setValue(newMiddleName);
        ref.child("users").child("student").child(uid).child("lastname").setValue(newLastName);
        progressBar.setVisibility(View.GONE);

    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(AccountSettingsActivity.this, "Failed to update name!", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
    }
});

以下是更新第二个和第三个框的代码:

ref.child("users").child("teacher").child("class").child("Listofstudents").child(user.getUid().toString().trim()).addListenerForSingleValueEvent(new ValueEventListener() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String uid = user.getUid();
    String newFirstName = oldEmail.getText().toString().trim();
    String newMiddleName = middlename.getText().toString().trim();
    String newLastName = lastname.getText().toString().trim();

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            ref.child("users").child("teacher").child(ref.child("teacher").getKey()).child("class").child(ref.child("class").getKey()).child("Listofstudents").child(user.getUid().toString().trim()).child("firstname").setValue(newFirstName);
            ref.child("users").child("teacher").child(ref.child("teacher").getKey()).child("class").child(ref.child("class").getKey()).child("Listofstudents").child(user.getUid().toString().trim()).child("middlename").setValue(newMiddleName);
            ref.child("users").child("teacher").child(ref.child("teacher").getKey()).child("class").child(ref.child("class").getKey()).child("Listofstudents").child(user.getUid().toString().trim()).child("lastname").setValue(newLastName);
            // TODO: handle the case where the data already exists
        }
        else {
            // TODO: handle the case where the data does not yet exist
        }
    }

    @Override
    public void onCancelled(DatabaseError firebaseError) {
        Toast.makeText(AccountSettingsActivity.this, "Failed to update name!", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
    }
});

1 个答案:

答案 0 :(得分:0)

您无需添加listener即可在Firebase数据库中进行更新。

请在此post查看我的回答。

希望它有所帮助。