为什么我的FirebaseRecylerAdapter无法正常工作? [英] Why My FirebaseRecylerAdapter Not Working Properly?

查看:170
本文介绍了为什么我的FirebaseRecylerAdapter无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示位于我的数据库树的所有子女姓名 。我使用@Alex Mamo给出的答案无法使用地图获取Firebase数据库中的所有子数据?
我想将所有数据显示到recyclerView中。但由于某些原因,我的屏幕手机上没有获得所有的名字(伊娃,史密斯,公主),我只能看到一个公主,这是在我的recyclerView布局(公主,公主,公主)显示3次。在玩过Alex的回答之后,我发现这在我的情况下是完美的,

  DatabaseReference yourRef = FirebaseDatabase.getInstance()。getReference ().child( 用户); 
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);





$ b

所以,因为这个答案在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;


$ b 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);


code
$ b

欢迎任何帮助或提示。
编辑:添加我的布局
这是我的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
$ b $ android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_gravity =center_horizo​​ntal
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>

$ b< 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
机器人:文字颜色= #AAA/>

< / RelativeLayout>

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

只是想知道我在说什么这是我正在获取的屏幕截图(公主,公主),当我应该得到(伊娃,史密斯,公主)。 我的ScreenShot 如果你仔细观察,可以看到Toast正在返回Eva,其他2个名字正确,但不在屏幕上。

解决方案


  displayName.setText(name); //在屏幕上显示


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

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



您需要填充一些ArrayList

 列表< String> names = new ArrayList<>(); (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中使用该列表,而不是

I am trying to display all child "name" located in My Database tree. I used this answer given by @Alex Mamo Unable to get All child data in Firebase Database using a Map? I want to display all the data into a recyclerView. But for some reasons instead of getting all names(Eva, smith, princess) on my Screen Phone, I am only able to see one "princess" which is being displayed 3 times in my recyclerView layout(princess, princess, princess). After playing around with Alex's answer, I found this worked perfectly in my case,

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

So since this answer give me all names correctly in a Toast but not on my screen, so I assume that something is wrong with my FirebaseRecyclerAdpater. Here my code so far,

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

Any Help or tip is welcome. EDIT: Adding my Layout This is my RecyclerView Layout

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

Lastly my Custom Layout where I want to display all Names

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

Just to have a Visual of what I am talking this a Screenshot of what I am getting(princess, princess, princess) when I should get (Eva,smith, princess). My ScreenShot If you look closer down you can see the Toast is returning "Eva" and 2 other names properly but not on Screen.

解决方案

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

You put this in a loop, where displayName is only a singluar UI element. In other words, you'll always only see the very last name from the loop.

If you notice, your linked post there doesn't have any loop

You need to populate some 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();

You can use that list in your RecyclerView, instead

这篇关于为什么我的FirebaseRecylerAdapter无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆