我想为Firebase数据做一个嵌套循环,但是结果是不正常的。我知道Firebase异步运行,但我不知道如何解决这个问题。请帮我解决这个问题。
守则:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recommend, container, false);
text = view.findViewById(R.id.test);
fuser = FirebaseAuth.getInstance().getCurrentUser();
getInfo();
return view;
}
private void getInfo() {
FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
assert user != null;
System.out.println("user " + user.getId() + "");
FirebaseDatabase.getInstance().getReference().child("Posts").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot datasnapshot : snapshot.getChildren()) {
Post post = datasnapshot.getValue(Post.class);
System.out.println("posts " + post.getPostid() + "");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}结果:
I/System.out:用户3 JERhGpZ1IYGswKZNOTJGY7mAU 13
I/System.out:用户AiHsIMRL4zd9YkdkMEhY0MRhHDU2
I/System.out:用户EXmknYFLpqcAHcvbcIUJYSTtDdl1
I/System.out:用户KV4tUuczuUbWLz3DR0gLn8STmF73
I/System.out:用户Lg8eMt56NOOAXx5PAPrYrbVnE533
I/System.out:用户QOMfQ4Vhk6cOniAsj2YnMnxRP8W2
I/System.out:用户Tnd20qA2g6VPr9s69ziPjou8K113
I/System.out:用户YsJCUD3lq2OsVknxcRqOTQNGFpP2
I/System.out:用户c8QABIe3zxNIZRCCbpvkHMd138x2
I/System.out:用户pBfZlyWmGrc6h4bcLLl4Re4MHe53
I/System.out:用户rRHFEuawGsMoUDzrIUaycBKy5653
I/System.out:用户xUWVqIoBKsaM8eq9pm1ky0YGm5R2
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
职位-MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
I/System.out: posts MMpqMPistmQyGm-_NpM
I/System.out: posts -MNTf7Hf6Eo_uB_jTXuw
I/System.out: posts -MNTluR0nSJARQ_tosL0
在我的Firebase中,我有12个用户和3个帖子,所以System.out.println("posts "+post.getPostid()+"");打印了36次。我希望代码打印每个用户的三个帖子作为每个嵌套循环在同步工作,而不是打印所有用户,然后打印帖子,所以,如何解决呢?
发布于 2020-12-07 15:40:40
一旦您给出了打印语句,就无法延迟它。因此,以正确的顺序获得输出的唯一方法是将print语句移动到您想要的数据可用的时候。
FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
assert user != null;
FirebaseDatabase.getInstance().getReference().child("Posts").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
System.out.println("user "+user.getId()+"");
for (DataSnapshot datasnapshot : snapshot.getChildren()) {
Post post = datasnapshot.getValue(Post.class);
System.out.println("posts "+post.getPostid()+"");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // don't ignore errors
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // don't ignore errors
}
}); https://stackoverflow.com/questions/65182469
复制相似问题