与用户的RecyclerView为空

时间:2019-06-23 17:00:26

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用Firebase和Android Studio创建一个聊天应用程序。我正在使用RecyclerView在Firebase数据库中查看仅具有其名字的所有用户,但每次都是空的。有人可以告诉我如何修改?

这是我的数据库的样子。 ![1] https://imgur.com/a/U06syNe

我尝试了很多事情,但似乎无济于事。尝试了许多来自github的教程。

UsersActivity类是RecyclerView和FirebaseRecyclerAdapter的位置

public class UsersActivity extends AppCompatActivity {
    private RecyclerView userslist;
    private DatabaseReference ref;
    private List<Users> mUsers;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_users );
        ref = FirebaseDatabase.getInstance ().getReference ().child ( "users" );


        userslist = (RecyclerView) findViewById ( R.id.users_list );


        userslist.setLayoutManager ( new LinearLayoutManager ( this ) );


    }


    @Override
    protected void onStart () {
        super.onStart ();
        FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users> ()
                .setQuery ( ref, Users.class )
                .build ();
        FirebaseRecyclerAdapter<Users, FindFriendViewHolder> adapter = new FirebaseRecyclerAdapter<Users, FindFriendViewHolder> ( options ) {
            @Override
            protected void onBindViewHolder (@NonNull FindFriendViewHolder findFriendViewHolder, int i, @NonNull Users users) {
                findFriendViewHolder.userName.setText ( users.getName () );

            }

            @NonNull
            @Override
            public FindFriendViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.users_template, parent, false );
                FindFriendViewHolder viewHolder = new FindFriendViewHolder ( view );
                return viewHolder;
            }
        };
        userslist.setAdapter ( adapter );
        adapter.startListening ();
    }

    public static class FindFriendViewHolder extends RecyclerView.ViewHolder {
        TextView userName;
        ImageView profileImage;

        public FindFriendViewHolder (@NonNull View itemView) {
            super ( itemView );
            userName = itemView.findViewById ( R.id.username1 );
            profileImage = itemView.findViewById ( R.id.profilepicture );

        }


    }
}

这是我为用户设计的模型

public class Users {
    public String name;
    public Users(){}
    public Users(String name){
    this.name=name;}
    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }


}


gradle看起来像这样(我正在使用androidx)

   implementation 'com.google.firebase:firebase-core:16.0.9'
   implementation 'com.google.firebase:firebase-firestore:19.0.2'
   implementation 'com.google.firebase:firebase-storage:17.0.0'
   implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
   implementation 'com.google.firebase:firebase-auth:17.0.0'
   implementation 'com.google.firebase:firebase-database:17.0.0'
   implementation 'androidx.legacy:legacy-support-v4:1.0.0'
   testImplementation 'junit:junit:4.13-beta-3'
   androidTestImplementation 'androidx.test:runner:1.2.0'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
   implementation 'com.firebaseui:firebase-ui-database:5.0.0'
   implementation 'com.android.support:multidex:1.0.3'

我希望从数据库中获取数据并存储在RecyclerView中以显示所有用户。

1 个答案:

答案 0 :(得分:1)

代码中没有显示getitemcount()方法。绝对有必要在recyclerview中实现该方法。