【问题标题】:Why My FirebaseRecylerAdapter Not Working Properly?为什么我的 FirebaseRecylerAdapter 无法正常工作?
【发布时间】:2017-05-25 21:35:49
【问题描述】:

我正在尝试显示位于My Database tree 中的所有子“名称”。我使用了@Alex Mamo Unable to get All child data in Firebase Database using a Map?给出的这个答案 我想将所有数据显示到 recyclerView 中。但由于某些原因,我只能在我的屏幕手机上看到所有名字(Eva、smith、公主),而我只能看到一个“公主”,它在我的 recyclerView 布局中显示了 3 次(公主、公主、公主)。在玩弄了亚历克斯的答案之后,我发现这对我来说非常有效,

 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 个名称,但屏幕上没有。

【问题讨论】:

  • 您能提供给我们您的 .XML 文件吗?
  • @AlexMamo 添加了请再看一遍
  • 尝试使用LinearLayout 更改您的Custom LayoutRelativeLayout,并将layout_widthlayout_height 设置为wrap_content,如下所示:android:layout_width="wrap_content"android:layout_height="wrap_content" 。有用吗?
  • 不。顺便说一句,我添加了一张图片,只是为了让您直观地了解我在说什么。感谢您尝试提供帮助
  • 尝试在您的FirebaseRecyclerAdapter 中将UsersPostClass 更改为String 类。

标签: android firebase firebase-realtime-database firebaseui


【解决方案1】:
displayName.setText(name);// display on the screen

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

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

你需要填充一些 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 中使用该列表,而不是

【讨论】:

  • 现在我让 [Eva, smith, princess] 像这样显示 3 次。显然答案是如此接近..
  • Toast 也一样
  • 就像我说的,你需要 1) 创建一个新的适配器 2) 在你的 RecyclerView 看到它...你不能在这里使用 FirebaseRecyclerView 库
  • 谢谢它现在工作正常我忘了用你的方法创建一个新的适配器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2020-06-27
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多