首页 > 解决方案 > 卡在试图从 firebase 数据库中检索数据

问题描述

我正在尝试检索存储在实时数据库中的值,但是当我打开活动时找不到值,我只能看到我输入的提示。

这是我的活动.java


    private DatabaseReference settingUsersRef;
    private FirebaseAuth mAuth;
    Button SaveInformation;
    private EditText FullName, UserName, DOB, Bio, Interest, Gender;
    //  private ImageView ProfileImage;
    private String currentUserID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testingactivity);

        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        settingUsersRef = FirebaseDatabase.getInstance().getReference().child("users").child(currentUserID);

        SaveInformation = findViewById(R.id.saveUserBtn);
        FullName = findViewById(R.id.fullNameText);
        UserName = findViewById(R.id.userNameText);
        DOB = findViewById(R.id.dobText);
        Bio = findViewById(R.id.bioText);
        Interest = findViewById(R.id.interestText);
        Gender = findViewById(R.id.genderText);

        settingUsersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                if (dataSnapshot.exists()) {
                    dataSnapshot.getChildren();
                    String myUsername = dataSnapshot.child("username").getValue().toString();
                    String myFullname = dataSnapshot.child("fullname").getValue().toString();
                    String myDOB = dataSnapshot.child("dob").getValue().toString();
                    String myBio = dataSnapshot.child("bio").getValue().toString();
                    String myInterest = dataSnapshot.child("interests").getValue().toString();
                    String myGender = dataSnapshot.child("gender").getValue().toString();
                    //          String myEmail = dataSnapshot.child("email").getValue().toString();

                    UserName.setText(myUsername);
                    FullName.setText(myFullname);
                    DOB.setText(myDOB);
                    Bio.setText(myBio);
                    Interest.setText(myInterest);
                    Gender.setText(myGender);
                }
                //    showData(dataSnapshot);

            }

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


            }
        });



    }
}

下面是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".testingactivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="25dp"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/usersImage"
                android:layout_width="218dp"
                android:layout_height="178dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="58dp"
                android:layout_marginEnd="100dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />

            <EditText
                android:id="@+id/userNameText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/usersImage"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:ems="10"
                android:hint="Username"
                android:inputType="textPersonName"
                android:textColor="@color/colorAccent"
                android:textColorHint="@color/colorAccent"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/fullNameText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/userNameText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:ems="10"
                android:hint="Full Name"
                android:inputType="textPersonName"
                android:textColor="@color/colorAccent"
                android:textColorHint="@color/colorAccent" />

            <EditText
                android:id="@+id/dobText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/fullNameText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:ems="10"
                android:hint="DOB mm/dd/yy"
                android:inputType="date"
                android:textColorHint="@color/colorAccent" />

            <EditText
                android:id="@+id/bioText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/dobText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:ems="10"
                android:hint="Bio"
                android:text=""
                android:textColor="@color/colorAccent"
                android:textColorHint="@color/colorAccent" />

            <EditText
                android:id="@+id/interestText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/bioText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:ems="10"
                android:hint="Interests"
                android:textColorHint="@color/colorAccent" />

            <EditText
                android:id="@+id/genderText"
                android:layout_width="218dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/interestText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="100dp"
                android:background="@drawable/text_background"
                android:backgroundTint="@color/colorblack"
                android:hint="Gender"
                android:textColor="@color/colorAccent"
                android:textColorHint="@color/colorAccent" />

            <Button
                android:id="@+id/saveUserBtn"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/genderText"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="14dp"
                android:layout_marginEnd="135dp"
                android:background="@android:color/transparent"
                android:text="Update Profile"
                android:textColor="@color/colorWhite"
                android:textSize="20dp" />


        </RelativeLayout>

    </ScrollView>


</RelativeLayout>

我从不同的 Java 活动向我的数据库中启动了虚拟变量,并且能够在数据库中看到这些值。我将附上一张我的数据库的照片,显示 uid 和与该 uid 一起使用的孩子。

在此处输入图像描述

标签: javaandroidfirebase-realtime-database

解决方案


推荐阅读