Firebase - 尝试将每个用户的数据检索到列表中 [英] Firebase - Trying to retrieve data of each user into a list
问题描述
实时数据库
中检索他的信息。 这是我的代码,以达到这个目的:
pre $ public $ check_For_Friends_And_Requests(){
String loggedUser = SaveSharedPreference.getLoggedEmail(getApplicationContext());
final DatabaseReference mDatabase = FirebaseDatabase.getInstance()。getReference();
final DatabaseReference userRef = mDatabase.child(Users)。child(loggedUser);
userRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()){$ b (DataSnapshot postSnapshot:dataSnapshot.child(friend_requests_received)。getChildren()){$ b(
$ final List< User> friendRequestsReceived_UserList = new ArrayList<>
$ b final String senderEmail = postSnapshot.getKey();
Toast.makeText(getApplicationContext(),$ b $ senderEmail,Toast.LENGTH_SHORT).show();
if(senderEmail!= null){
mDatabase.child(Users)。child(senderEmail).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Toast.makeText(getApplicationContext(),
dataSnapshot.child(name)。getValue(String.class),Toast.LENGTH_SHORT).show();
friendRequestsReceived_UserList.add(
new User(
senderEmail,
dataSnapshot.child(name).getValue(String.class),
dataSnapshot .child(level)。getValue(Integer.class),
dataSnapshot.child(skill)。getValue(Double.class)));
$ @覆盖
public void onCancelled(DatabaseError databaseError){
});
$ b UserListAdapter friendRequestsReceived_Adapter =
UserListAdapter(getApplicationContext(),
R.layout.friend_requests_received_listview_row,
friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
}
else
connectionErrorGoToMain();
$ b @Override
public void onCancelled(DatabaseError databaseError){
connectionErrorGoToMain();
}
});
$ b我已经在这个代码中2 code> ValueEventListeners 。我将用户信息添加到内部列表中。 问题是这个过程结束时列表是空的。
我想用这些信息来填充列表视图:
$ $ p $ UserListAdapter friendRequestsReceived_Adapter = $ b $ new UserListAdapter(getApplicationContext(),
R.layout.friend_requests_received_listview_row,
friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
当我将它们放入innner侦听器时,它工作正常,但我不想设置适用于列表中每个用户的适配器,仅在 for
循环之后。
我的数据库结构(我不需要得到所有的参数):
列表是空的,因为您声明的是 friendRequestsReceived_UserList
在内部 onDataChange()
方法之外。发生这种情况的原因是 onDataChange()
方法在将这些新对象添加到列表之前被调用。所以,为了解决这个问题,只需要在这个内部 onDataChange()
方法内移动列表的声明就可以了:
if(senderEmail!= null){
mDatabase.child(Users)。child(senderEmail).addListenerForSingleValueEvent(new ValueEventListener(){
@覆盖
public void onDataChange(DataSnapshot dataSnapshot){
final List< User> friendRequestsReceived_UserList = new ArrayList<>(); // Moved here
Toast.makeText(getApplicationContext ),dataSnapshot.child(name)。getValue(String.class),Toast.LENGTH_SHORT).show();
friendRequestsReceived_UserList.add(
new User(
senderEmail,
dataSnapshot.child(name)。getValue(String.class),
dataSnapshot.child(level)。getValue(Integer.class),
dataSnapshot.child skill)。getValue(Double.class)));
UserListAdapter friendRequestsReceived_Adapter =
new UserListAdapter(getApplicationContext(),R.layout.friend_requests_received_listview_row,friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
$ @覆盖
public void onCancelled(DatabaseError databaseError){
});
正如您所看到的,我也将该适配器设置在内部方法中。如果你想在 onDataChange()
之外使用这个列表,我建议你阅读我的答案帖子。
Each user in my app can send and get friend requests. When the user checks his friends requests, I want the app to go through each user who sent him a friend request and retrieve his information from the Realtime Database
.
This is my code in order to accomplish this:
public void check_For_Friends_And_Requests(){
String loggedUser=SaveSharedPreference.getLoggedEmail(getApplicationContext());
final DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference();
final DatabaseReference userRef=mDatabase.child("Users").child(loggedUser);
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final List<User> friendRequestsReceived_UserList=new ArrayList<>();
for (DataSnapshot postSnapshot: dataSnapshot.child("friend_requests_received").getChildren()) {
final String senderEmail=postSnapshot.getKey();
Toast.makeText(getApplicationContext(),
senderEmail, Toast.LENGTH_SHORT).show();
if (senderEmail!=null){
mDatabase.child("Users").child(senderEmail).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getApplicationContext(),
dataSnapshot.child("name").getValue(String.class), Toast.LENGTH_SHORT).show();
friendRequestsReceived_UserList.add(
new User(
senderEmail,
dataSnapshot.child("name").getValue(String.class),
dataSnapshot.child("level").getValue(Integer.class),
dataSnapshot.child("skill").getValue(Double.class)));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
UserListAdapter friendRequestsReceived_Adapter =
new UserListAdapter(getApplicationContext(),
R.layout.friend_requests_received_listview_row,
friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
}
else
connectionErrorGoToMain();
}
@Override
public void onCancelled(DatabaseError databaseError) {
connectionErrorGoToMain();
}
});
}
I have in this code 2 ValueEventListeners
. I add the user information to the list in the inner one. The problem is that the list is empty at the end of this process.
I would like to fill a list view with this information using these lines:
UserListAdapter friendRequestsReceived_Adapter =
new UserListAdapter(getApplicationContext(),
R.layout.friend_requests_received_listview_row,
friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
When I put them inside the innner listener, it works fine, but I don't want to set the adapter for each user in the list, only after the for
loop.
I'm attaching a screenshot with my database structure (I don't need to get all of the parameters):
The list is empty because you are declaring friendRequestsReceived_UserList
outside the inner onDataChange()
method. This is happening due the asynchronous behaviour of onDataChange()
method which is called before you are adding those new objects to the list. So, in order to solve this, just move the declaration of the list inside the inner onDataChange()
method like this:
if (senderEmail!=null){
mDatabase.child("Users").child(senderEmail).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<User> friendRequestsReceived_UserList=new ArrayList<>(); //Moved here
Toast.makeText(getApplicationContext(), dataSnapshot.child("name").getValue(String.class), Toast.LENGTH_SHORT).show();
friendRequestsReceived_UserList.add(
new User(
senderEmail,
dataSnapshot.child("name").getValue(String.class),
dataSnapshot.child("level").getValue(Integer.class),
dataSnapshot.child("skill").getValue(Double.class)));
UserListAdapter friendRequestsReceived_Adapter =
new UserListAdapter(getApplicationContext(), R.layout.friend_requests_received_listview_row, friendRequestsReceived_UserList);
friendRequestsReceived_ListView.setAdapter(friendRequestsReceived_Adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
As you probably see, i set the adapter also inside the inner method. If you want to use that list outside the onDataChange()
i suggest you reading my answer from this post.
这篇关于Firebase - 尝试将每个用户的数据检索到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!