In Android studio, I want function getUsers to execute before getPosts,
but the execution order is wrong,
and in my firebase real time database, I have 12 users,only two "fff" printed in logcat,
how to solve the problem??
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recommend, container, false);
text = view.findViewById(R.id.test);
fuser = FirebaseAuth.getInstance().getCurrentUser();
getUsers();
getPosts();
return view;
}
getUsers()
private void getUsers() {
FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(newValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.e("si", "fff" + "");
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
Log.e("si", "fff" + "");
users.add(user.getId());
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
getPosts()
private void getPosts() {
FirebaseDatabase.getInstance().getReference().child("Posts").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.e("p", "pp" + "");
Post post = snapshot.getValue(Post.class);
posts.add(post.getPostid());
}
dataSet();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
the logcat:
1607174523.915 21527-21527/com.example.signup E/p: pp
1607174523.915 21527-21527/com.example.signup E/p: pp
1607174523.915 21527-21527/com.example.signup W/ClassMapper: No setter/field for isslod found on class com.example.signup.Model.Post
1607174523.916 21527-21527/com.example.signup E/p: pp
1607174524.212 21527-21527/com.example.signup E/si: fff
1607174524.219 21527-21527/com.example.signup I/chatty: uid=10159(com.example.signup) identical 11 lines
1607174524.219 21527-21527/com.example.signup E/si: fff
1607174525.183 21527-21610/com.example.signup V/FA: Inactivity, disconnecting from the service