0

I'm using Android studio -Java

from the images below, which show my firebase, I want to fill an integer matrix which represents users in rows and posts in columns. and the values of the matrix will represent which posts the users likes and which posts the user not put like on it . ( 1 if the users like it and 0 if not).

"Likes" collection in firebase contains the 'Postid' and the children of postid are the 'userId' for each user like this post.


the problem is that when I make a nested for loop to fill the matrix, the results is wrong because firebase is Asynchronous and I want the nested loops to run synchronously to fill the matrix in the correct order . I used callbacks but I don't know a lot about dealing with it.

Is there anyone who can help me please?

user collection-firebase

post collection-firebase

like collection-firebase

the code:

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

        getUsers(new UserListCallback() {
            @Override
            public void onCallback(ArrayList<String> users) {
                Log.e("users", users.size()+"" );
            }
        });

        return view;
    }


private void getUsers(final UserListCallback myCallback) {

        FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.e("si", "hff" + "");
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    User user = snapshot.getValue(User.class);
                    users.add(user.getId());
                }
                myCallback.onCallback(users);
                 getPosts(new PostListCallback() {
                        @Override
                        public void onCallback(ArrayList<String> users) {
                            Log.e("posts", posts.size()+"" );
                            // System.out.println("Loaded "+" contacts");
                        }
                    });
                Log.e("si", "fff" + "");
            }



            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }


        });
    }
private void getPosts(final PostListCallback myCallback) {
        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());
                }
                myCallback.onCallback(posts);
                prepareArray(new ArrayCallback() {
                    @Override
                    public void onCallback(ArrayStructure data) {
                        Log.e("posts", posts.size()+"" );
                        // System.out.println("Loaded "+" contacts");
                    }
                });
            //    prepareArray(final UserListCallback myCallback);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }


        });
    }
private void prepareArray(final ArrayCallback myCallback) {
        for(int i=0 ;i<users.size();i++){
            for(int j=0;j<posts.size();j++){
                
                int finalI = i;
                int finalJ = j;

                FirebaseDatabase.getInstance().getReference().child("Likes").addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {

                        if(snapshot.child(posts.get(finalJ)).exists()){
                            Log.e("j", finalJ+": "+posts.get(finalJ) );

                            FirebaseDatabase.getInstance().getReference().child("Likes").child(posts.get(finalJ)).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange (@NonNull DataSnapshot d4snapshot){

                                            if (d4snapshot.child(users.get(finalI)).exists()) {
                                                Log.e("i", finalI+": "+users.get(finalI) );

                                                Data.add(finalI, finalJ, 1.0);
                                            } else {
                                                Log.e("i", finalI+": "+users.get(finalI) );

                                                Data.add(finalI,
                                                        finalJ,
                                                        0.0);
                                            }
                                            //   Log.e("post", j+ "");

                                        }


                                        @Override
                                        public void onCancelled (@NonNull DatabaseError error){

                                        }
                                    });
                        }
                        else {
                            Data.add(finalI,
                                    finalJ,
                                    0.0);
                            Log.e("j", finalJ+": "+posts.get(finalJ) );

                        }
                 //

                    }


                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
        }
        myCallback.onCallback(Data);
        data= Data.toArray();
        for(int l=0 ;l<data.length;l++){
           // Log.e("user", users.size()+"" );
            for(int f=0;f<data[l].length;f++){
                Log.e("post", posts.size()+"" );
                Log.e("hi", data[l][f]+"" );
            }
        }
    }

roro roor
  • 137
  • 1
  • 4
  • 8
  • 1
    "Is there anyone who can help me and write the code please?" That's not how Stack Overflow works. You will have to show the minimal code that reproduces where you got stuck. Otherwise there's not much more we can say than I've done here: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Dec 06 '20 at 15:50
  • @FrankvanPuffelen I edited the post and add the code, if you can help me please help – roro roor Dec 06 '20 at 17:10

0 Answers0