【发布时间】:2018-11-14 01:56:47
【问题描述】:
简而言之:我有一个用户 ID 列表,我想遍历数据库并找到这些用户的配置文件并将它们放在列表中。但是我有一个问题如下:
final List<Friend> friendsProfiles = new ArrayList<>();
for (final FriendId friendId : friendIds) {
mUserRef.child(friendId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Get the friend profile
Friend friend = dataSnapshot.getValue(Friend.class);
// Add to the list
friendsProfiles.add(friend);
// The problem is here, because its called as many times as the size of
// the friendIds list. loadnewData() contains notifyDataSetChanged()
mFriendsFragment.loadNewData(friendsProfiles);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// It gives 0, because it's called before onDatachange()
// So I can't call loadNewData() here
Log.d(TAG, updatedFriendsRequestList.size());
如何以正确、正确的方式做到这一点?
【问题讨论】:
-
你的
friendIds列表是什么类型的? -
为了更好地理解,我简化了这个案例。最初它包含:字符串friendId和朋友邀请的类型(“已发送”或“已接收”)。所以它基本上是包含 2 个字符串的对象列表。
-
所以基本上你的
friendIds列表被声明为List<String> friendIds = new ArrayList<>();,对吧? -
没有。在这种情况下,它是对朋友的邀请列表。它看起来像这样:
List<FriendRequest> requests= new ArrayList<>();其中 FriendRequest 是一个对象,其中包含String frienId和String requestType(“已发送”或“已接收”)他发送或接收的邀请,这决定了要使用的布局。 -
感谢您的解释。还有一件事,请告诉我您的
friendIds列表是如何声明的。谢谢!
标签: android firebase firebase-realtime-database android-recyclerview