从Firebase数据库填充RecyclerView

时间:2018-10-27 18:32:30

标签: java android firebase android-recyclerview firebaseui

我刚接触Android,正在尝试将Firebase数据库中的数据显示到Recyclerview,但是当我运行该应用程序时,“用户活动”为空白,或者RecyclerView上没有任何显示。

我被卡住了,但输出还是一样的:什么也没显示。

UsersActivity:

private Toolbar mToolbar;

private RecyclerView mUsersList;

private DatabaseReference mUsersDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);

    mToolbar = (Toolbar) findViewById(R.id.users_appBar);
    setSupportActionBar(mToolbar);

    getSupportActionBar().setTitle("Users");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mToolbar.setTitleTextColor(Color.WHITE);
    mToolbar.setSubtitleTextColor(Color.WHITE);

    mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

    mUsersList = (RecyclerView) findViewById(R.id.users_list);
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new LinearLayoutManager(this));

}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
            Users.class,
            R.layout.users_single_layout,
            UsersViewHolder.class,
            mUsersDatabase

    ) {
        @Override
        protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
            usersViewHolder.setName(users.getName());
        }

    };
    mUsersList.setAdapter(firebaseRecyclerAdapter);
}


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

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

        mView = itemView;

    }
    public void setName(String name)
    {
        TextView userNameView = (TextView) mView.findViewById(R.id.user_name);
        userNameView.setText(name);
    }
}

这是我在Model类中的代码,即Users

private String name;
private String image;
private String status;

public Users()
{

}

public Users(String name, String image, String status) {
    this.name = name;
    this.image = image;
    this.status = status;
}

public String getName() {
    return name;
}

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

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

1 个答案:

答案 0 :(得分:0)

您使用的是旧版本的FirebaseUI,需要删除mUsersList.setHasFixedSize(true);才能显示数据