我想从firebase数据库中检索四个值作为字符串,并将它们添加到四个textview中

时间:2017-07-26 20:26:45

标签: android firebase firebase-realtime-database

我目前有我的代码只检索一个,我不想在列表视图中,只需要两个文本视图。

检索一个代码:

fRef = new Firebase("https://sw1ft13s-6b875.firebaseio.com/users/" + userID);
fRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
        String kills = dataSnapshot.getValue(String.class);
        textViewUserkills.setText(kills);    `

它只检索了一个,但我有多个textview要在布局文件中检索

布局文件是:

</FrameLayout>
    <LinearLayout
        android:id="@+id/linear"
        android:layout_below="@+id/head"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentStart="true">
        <customfonts.MyTextView
            android:layout_weight="1"
            android:id="@+id/textViewUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Godwin Jerome"
            android:textStyle="bold"
            android:textSize="25sp"
            android:textColorHint="#181737"
            android:textColor="#181737"
            android:background="#0000"
            android:padding="3dp"
            android:gravity="center" />

            <customfonts.MyTextView
                android:layout_weight="1"
                android:id="@+id/textViewUserkills"
                android:textSize="16sp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="---"
                android:textColorHint="#181737"
                android:textColor="#181737"
                android:maxLength="12"
                android:background="#0000"
                android:padding="2dp"
                android:gravity="center" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="5dp">
            <customfonts.MyTextView
                android:layout_weight="1"
                android:id="@+id/textViewUserAddress"
                android:textSize="12sp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Abeka Junction"
                android:textColorHint="#181737"
                android:textColor="#181737"
                android:padding="2dp"
                android:background="#0000"
                android:gravity="center" />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="#dad8d8"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#ffffff">
            <customfonts.MyTextView
                android:layout_weight="1"
                android:id="@+id/mail"
                android:textSize="16sp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Email"
                android:textColor="#a2a1b8"
                android:maxLength="12"
                android:background="#0000"
                android:padding="16dp"
                android:gravity="start"/>

            <customfonts.MyTextView
                android:id="@+id/textViewUserEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#0000"
                android:gravity="end"
                android:hint="email@kalajob.com"
                android:padding="16dp"
                android:textColor="#181737"
                android:textColorHint="#1b193b"
                android:textSize="16sp" />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#dad8d8"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#ffffff">
            <customfonts.MyTextView
                android:layout_weight="1"
                android:id="@+id/pswrd"
                android:textSize="16sp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Phone Number"
                android:textColor="#a2a1b8"
                android:maxLength="12"
                android:background="#0000"
                android:padding="16dp"
                android:gravity="start" />

            <customfonts.MyTextView
                android:id="@+id/textViewPhoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#0000"
                android:gravity="end"
                android:hint="054-321-9876"
                android:inputType="numberSigned"
                android:maxLength="12"
                android:padding="16dp"
                android:textColor="#181737"
                android:textColorHint="#1b193b"
                android:textSize="16sp" />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="#dad8d8"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/recm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:orientation="vertical"
                android:padding="8dp"
                android:paddingStart="8dp">

                <customfonts.MyTextView
                    android:id="@+id/sin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="1dp"
                    android:text="6"
                    android:textColor="#008cc9"
                    android:textSize="20sp" />

                <customfonts.MyTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="1dp"
                    android:text=" Recommends"
                    android:textColor="#181737"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/feedb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:orientation="vertical"
                android:paddingEnd="100dp"
                android:padding="8dp"
                android:paddingStart="8dp">

                <customfonts.MyTextView
                    android:id="@+id/sinn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="1dp"
                    android:text="3"
                    android:textColor="#008cc9"
                    android:textSize="20sp" />

                <customfonts.MyTextView
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:padding="1dp"
                    android:text=" Feedback"
                    android:textColor="#181737"
                    android:textSize="15sp" />
            </LinearLayout>
        </RelativeLayout>

我的数据库结构是:

     sw1ft13s-6b875
       |---users
         |---26wr5p82UIhUkbv5VN00q9U0r3J2
                |---kills: "955"
                |---location: "Outside"
                |---name: "Godwin Nova"
                |---phoneNumber: "05477772586"    

帮助?其他textview是姓名,地址,电话号码,评级。  提前超级谢谢。

2 个答案:

答案 0 :(得分:0)

使用

fRef = new Firebase("https://sw1ft13s-6b875.firebaseio.com/users/" + userID);
fRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
        String kills = dataSnapshot.child("kills").getValue(String.class);
        textViewUserkills.setText(kills);
        String location=dataSnapshot.child("location").getValue(String.class);

等等

答案 1 :(得分:0)

为此,最好的方法就是

  

What is the difference between ChildEventListener and ValueEventListener Firebase interfaces?

     
    

通过将侦听器更改为valueEventListener(),检索整个侦听器     每个值变化的数据。

  
fRef = new Firebase("https://sw1ft13s-6b875.firebaseio.com/users/" + userID);
fRef.addValueEventListener(new com.firebase.client.ValueEventListener() {
        @Override
        public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
            //String kills = dataSnapshot.getValue(String.class);
            String kills = dataSnapshot.child("kills").getValue(String.class);
            textViewUserkills.setText(kills);
            String location=dataSnapshot.child("location").getValue(String.class);
            textViewUserAddress.setText(location);

等等! 感谢Martin De Simone&#39;

的开始