为什么我的 FirebaseRecylerAdapter 不能正常工作?

Why My FirebaseRecylerAdapter Not Working Properly?

我正在尝试显示位于 My Database tree. I used this answer given by @Alex Mamo 的所有 child "name" 我想将所有数据显示到 recyclerView 中。但由于某些原因,我没有在屏幕上显示所有名称(Eva、smith、princess)Phone,我只能看到一个 "princess" 在我的 recyclerView 布局中显示了 3 次(princess,公主,公主)。在尝试了 Alex 的回答后,我发现这对我来说非常有效,

 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);
    }

因此,由于此答案在 Toast 中正确地给出了所有名称,但在我的屏幕上却没有,所以我假设我的 FirebaseRecyclerAdpater 有问题。到目前为止,这是我的代码,

FirebaseRecyclerAdapter<UsersPostClass,  UsersViewHolder> userList = new FirebaseRecyclerAdapter<UsersPostClass,  UsersViewHolder>(
            UsersPostClass.class,
            R.layout.custom,
             UsersViewHolder.class,
            mDatabaseRef
    ) {
        @Override
        protected void populateViewHolder( UsersViewHolder viewHolder, UsersPostClass model, int position) {
            viewHolder.setUsernameList(getApplicationContext());


        }
    };
    mRecyclerView.setAdapter(userList);
}

private static class  UsersViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public  UsersViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

    }

    public void setUsernameList(final Context context) {
        final TextView displayName = (TextView) mView.findViewById(R.id.listViewUsername);
        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);

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

                }
            }

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

欢迎任何帮助或提示。 编辑:添加我的布局 这是我的 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" />

最后是我要显示所有名称的自定义布局

<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>

只是为了直观地了解我在说什么,这是我在应该得到(伊娃、史密斯、公主)时得到的(公主、公主、公主)的屏幕截图。 My ScreenShot 如果仔细观察,您会看到 Toast 正在正确返回 "Eva" 和其他 2 个名称,但不在屏幕上。

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

你把它放在一个循环中,其中 displayName 只是一个 singluar UI 元素。换句话说,您将始终只能看到循环中的最后一个名字。

如果您注意到,您的链接 post 没有任何循环

您需要填充一些 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();

您可以在 RecyclerView 中使用该列表,而不是