在这个启动方法中,我尝试显示Firebase实时数据库中所有用户的列表,但它根本没有显示任何用户。日志猫上没有错误。
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mDatabase, Users.class)
.build();
Toast.makeText(this, "HI", Toast.LENGTH_SHORT).show();
FirebaseRecyclerAdapter<Users,useersViewholder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, useersViewholder>(options) {
@Override
protected void onBindViewHolder(@NonNull useersViewholder holder, int position, @NonNull Users model) {
holder.setName(model.getName());
}
@Override
public useersViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
Toast.makeText(UsersActivity.this, "Inside onCreateViewHolder", Toast.LENGTH_SHORT).show();
View v=LayoutInflater.from(UsersActivity.this).inflate(R.layout.users_single_row,parent,false);
return new useersViewholder(v);
}
};
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
firebaseRecyclerAdapter.notifyDataSetChanged();
mUserList.setAdapter(firebaseRecyclerAdapter);
}
public static class useersViewholder extends RecyclerView.ViewHolder{
View MItemView;
public useersViewholder(View itemView) {
super(itemView);
MItemView=itemView;
}
public void setName(String name) {
TextView mUsername=MItemView.findViewById(R.id.users_DisplayName);
mUsername.setText(name);
}
}
}
您好,我想您的问题是由于(未在适配器上调用 startListening(。
所以在onStart方法的这一行下:
mUserList.setAdapter(firebaseRecyclerAdapter);
添加这个
firebaseRecyclerAdapter.startListening();
希望对您有所帮助。