因此,在我的 for 循环中,我从一个路径检索值,然后在查询中,我使用第一个 for 循环中的 CreatorUid 从另一个路径获取其他字符串。我需要找到一种方法来检索这些字符串并将它们添加到位于第一个 for 循环中的数组列表中,而不是嵌套的 for 循环中。如果您需要澄清我在说什么,我在下面的代码中有评论:
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
chatsItemArrayList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
title = "" + ds.child("title").getValue();
description = "" + ds.child("description").getValue();
date = "" + ds.child("datecreated").getValue();
speaks = "" + ds.child("speaks").getValue();
upvotes = "" + ds.child("upvotes").getValue();
views = "" + ds.child("views").getValue();
picture = "" + ds.child("chatpicture").getValue();
String identifier = "" + ds.child("identifier").getValue();
creatorUid = "" + ds.child("chatcreatoruid").getValue();
System.out.println(creatorUid + " THIS IS THE CREATOR UID");
Query query = FirebaseDatabase.getInstance().getReference().child("Users").orderByKey().equalTo(creatorUid);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
//HOW DO I GET THESE VALUES FROM HERE:
rank = "" + dataSnapshot1.child("rank").getValue();
profilePicture = "" + dataSnapshot1.child("image").getValue();
username = "" + dataSnapshot1.child("username").getValue();
System.out.println("THIS IS THE RANK " + getRankText() + " THE USERNAME " + getUsernameText() + " PROFILEPICTURE " + getProfilePicture());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
System.out.println("R1" + getRankText() + " U1" + getUsernameText() + " P1 " + getProfilePicture());
// TO HERE:
chatsItemArrayList.add(new ChatsItem(getProfilePicture(), title, date, speaks, description, getUsernameText(), getRankText(), upvotes, views, picture));
}
mAdapter = new ChatsFragmentAdapter(chatsItemArrayList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
最佳答案
来自 Firebase 的数据仅在 onDataChange
方法内或从该方法调用的代码中可用。对此没有解决方法。
如果您需要额外的数据库调用来加载其他数据,您应该:
- 要么首先推迟将项目添加到列表中,然后仅将其添加到内部
onDataChange
- 或者先将不完整的项目添加到列表中,然后在内部
onDataChange
中更新它
所以最简单的修复方法是执行第一个,如下所示:
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
rank = "" + dataSnapshot1.child("rank").getValue();
profilePicture = "" + dataSnapshot1.child("image").getValue();
username = "" + dataSnapshot1.child("username").getValue();
chatsItemArrayList.add(new ChatsItem(getProfilePicture(), title, date, speaks, description, getUsernameText(), getRankText(), upvotes, views, picture));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
您通常还需要通过调用 adapter.notifyDataSetChanged()
等方法来刷新内部 onDataChange
方法中的适配器。
另请参阅:
关于java - 如何从用于 addValueEventListener 的 for 循环内的查询获取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60459468/