【问题标题】:Collect all data from FirebaseDatabse and call notifyDataSetChanged() once从 FirebaseDatabse 收集所有数据并调用 notifyDataSetChanged() 一次
【发布时间】: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&lt;String&gt; friendIds = new ArrayList&lt;&gt;();,对吧?
  • 没有。在这种情况下,它是对朋友的邀请列表。它看起来像这样:List&lt;FriendRequest&gt; requests= new ArrayList&lt;&gt;(); 其中 FriendRequest 是一个对象,其中包含 String frienIdString requestType(“已发送”或“已接收”)他发送或接收的邀请,这决定了要使用的布局。
  • 感谢您的解释。还有一件事,请告诉我您的friendIds 列表是如何声明的。谢谢!

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

您可以简单地计算您已经加载了多少,然后仅在加载最后一个后调用notifyDataSetChanged()

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);

            if (friendsProfiles.size() == friendIds.length) {
                mFriendsFragment.loadNewData(friendsProfiles);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException(); // don't ignore errors, as they break the logic of your app
        }
    });
}

【讨论】:

  • 如果列表包含1000个id,是否可以添加1000次监听?
  • 这似乎是一个不同的问题。但是你所有的听众都是短暂的,所以我怀疑这会造成问题。如果是这样(或者你很担心你想确保它没有),限制你添加下一个监听器的频率:所以添加前 10 个左右,然后只在另一个完成时添加下一个(或被取消)。
  • 我从这条评论中得到了我今天工作中最大问题之一的答案。谢谢!!!!我会试试你的解决方案。 +1
  • @IoanaP。我知道这是一个糟糕的解决方案,但我找不到更好的解决方案。你有什么建议?
  • @Frank van Puffelen 谢谢,这是一个简单的解决方案,为什么我没有想到它。但我会等待,也许有人会发布另一个解决方案。
猜你喜欢
  • 2021-08-26
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多