尝试从Firebase数据库检索数据时停滞了

时间:2019-03-03 21:15:30

标签: java android firebase-realtime-database

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

这是我的活动。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一起使用的子项。

enter image description here

0 个答案:

没有答案