问题描述
我正在尝试从RealTime-Database中检索其名字和名字的用户列表,但是什么也没有出现.
I'm trying to retrieve from RealTime-Database the list of users with their First name and second name but nothing appears.
UsersActivity类显示数据库中的所有用户.我尝试了很多教程,但似乎无济于事.
The class UsersActivity show all the users from the database.I tried a lot of tutorials but nothing seems to work.
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 ( "FirstName" );
userslist = (RecyclerView) findViewById ( R.id.users_list );
userslist.setHasFixedSize ( true );
userslist.setLayoutManager ( new LinearLayoutManager ( this ) );
}
public void readUsers(){
FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
DatabaseReference reference=FirebaseDatabase.getInstance ().getReference ("spors-budy-en866");
reference.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
} )
}
@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 );
}
}
}
我的数据库规则:
{
"rules": {
".read": "true",
".write":"true",
"users":{
"$user_id":{
".read": "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
}
我希望显示数据库中的所有用户.但是活动为空.
I expect to show all the users from my databse.But the activity is empty.
推荐答案
您正在设置:
ref = FirebaseDatabase.getInstance().getReference().child("FirstName");
然后使用它来初始化FirebaseUI适配器:
And then use that to initialize the FirebaseUI adapter:
FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users> ()
.setQuery(ref, Users.class )
虽然我不知道您的数据模型,但似乎不太可能在/FirstName
下有一个用户列表,这是使用FirebaseUI适配器显示这些用户所必需的.
While I don't know your data model, it seems unlikely that you'd have a list of users under /FirstName
, which is what is required to show those users with the FirebaseUI adapter.
更可能将ref初始化为:
More likely ref should be initialized as:
ref = FirebaseDatabase.getInstance().getReference("users")
这篇关于用户列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!