在这个On start方法中,我试图从Firebase实时数据库中显示所有用户的列表,但它根本没有显示任何用户。 Logcat没有错误。
@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);
}
}
}
答案 0 :(得分:0)
您好我猜您的问题是由于(不在您的适配器上调用startListening)。
所以在onStart方法的这一行下:
mUserList.setAdapter(firebaseRecyclerAdapter);
添加此
firebaseRecyclerAdapter.startListening();
希望它有所帮助。