在android中使用FirebaseUI for firebase recyclelerView?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (94)

这是我的代码示例:

public class UsersActivity extends AppCompatActivity {

private Toolbar userToolbar;
private RecyclerView usersList;

private DatabaseReference mUsersDatabase;

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

    userToolbar = (Toolbar) findViewById(R.id.users_toolbar);
    setSupportActionBar(userToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Users");

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

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

}

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

    FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
            Users.class,
            R.layout.single_user_list_layout,
            UsersViewHolder.class,
            mUsersDatabase
    ) {
        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {

            holder.setName(model.getName());

        }

        @NonNull
        @Override
        public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return null;
        }
    };

    usersList.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.single_user_name);
        userNameView.setText(name);
    }

}}

我得到的错误是:

提问于
用户回答回答于
Sorry wanted to comment but i ain't got enough points to do so. 
Try this first, if it doesnt work try using FirebaseRecyclerOptions 
(The latest   version (3.x) of FirebaseUI implements a different method ofinitializing a FirebaseRecyclerAdapter than previous versions.)    

Make your " firebaseRecyclerAdapter "  variable global, so you would have something like this.


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

扫码关注云+社区

领取腾讯云代金券