2017-05-25 17 views
0

My Database treeにあるすべての子「名前」を表示しようとしています。 @Alex Mamoによって与えられたこの答えを使用しましたUnable to get All child data in Firebase Database using a Map? 私はすべてのデータをrecyclerViewに表示したいと思います。しかし私のScreen Phoneですべての名前(Eva、smith、princess)を取得するのではなく、私はrecyclerViewレイアウト(プリンセス、プリンセス、プリンセス)に3回表示されている「プリンセス」しか見ることができません。アレックスの答えで遊んでた後、この答えは私に正しくすべての名前トーストではなく、私の画面上を与えるので、私はそう、これは私の場合には完全に働いたなぜFirebaseRecylerAdapterが正しく機能しないのですか?

DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("Users"); 
     ValueEventListener eventListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 
        String name = dataSnapshot1.child("name").getValue().toString(); 
        displayName.setText(name);// display on the screen 

        Toast.makeText(context, name, Toast.LENGTH_LONG).show(); 

       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) {} 
     }; 
     yourRef.addListenerForSingleValueEvent(eventListener); 
    } 

を見つけたので、私は何かが私が間違っていることを前提としていFirebaseRecyclerAdpater。ここまで私のコードは、

すべてのヘルプやヒントは歓迎です。 EDIT:これは私はちょうど私が何のビジュアルを持っているすべての名前

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardBackgroundColor="@android:color/transparent" 
    card_view:cardElevation="0dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="20dp"> 


     <TextView 
      android:id="@+id/listViewUsername" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:gravity="center_vertical" 
      android:text="Username" 
      android:textColor="#aaa"/> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

を表示したい私のRecyclerViewレイアウト

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="20dp" 
    android:text="My Players List" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" 
    android:textSize="20sp" 
    android:layout_marginBottom="10dp"/> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

最後に私のカスタムレイアウトである私のレイアウト を追加しますこれを話す私が(エバ、スミス、プリンセス)を取得する必要があるときに私は(プリンセス、プリンセス、プリンセス)を取得しているのスクリーンショット。 My ScreenShotもう少し見ると、Toastが「Eva」と他の2つの名前を正しく返していますが、Screenには戻っていないことがわかります。

+0

代わりに、あなたのRecyclerViewにそのリストを使用することができますいくつかのArrayList

List<String> names = new ArrayList<>(); for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { String name = dataSnapshot1.child("name").getValue().toString(); names.add(name); } displayName.setText(names.toString()); Toast.makeText(context, names.toString(), Toast.LENGTH_LONG).show(); 

を移入する必要がある任意のループ

がすることができますがありません。あなたに.XMLファイルを提供しますか? –

+0

@AlexMamo Plsをもう一度見てください –

+0

'Custom Layout'の' RelativeLayout'を 'LinearLayout'で変更し、' layout_width'と 'layout_height'を' wrap_content'としてみましょう: 'android:layout_width = "wrap_content" 'と' android:layout_height = "wrap_content" 'となります。それは動作しますか? –

答えて

0
displayName.setText(name);// display on the screen 

あなたはdisplayNameのみsingluar UI要素であるループ、でこれを置きます。言い換えれば、ループの最後の名前しか表示されません。

あなたが気付いた場合、リンクされた後のあなたは

+0

今、私は[エバ、スミス、プリンセス]が3回そのように表示されています。明らかに答えはとても近いです。 –

+0

トーストと同じすぎます –

+0

1)新しいアダプタを作成する必要があります2)RecyclerViewに見せてください... FirebaseRecyclerViewライブラリはここでは使用できません –

関連する問題