title: fill matrix from firebase data in android studio
body: <p>I'm using Android studio -Java<br><br>
  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).
  <br><br>
  &quot;Likes&quot; collection in firebase contains the 'Postid' and the children of postid are the 'userId' for each user like this post.</p>
  <br>
  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. 
  <p>Is there anyone who can help me please?</p>
  <p><a href="https://i.stack.imgur.com/MOCCI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MOCCI.jpg" alt="user collection-firebase" /></a></p>
  <p><a href="https://i.stack.imgur.com/CfvUm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CfvUm.png" alt="post collection-firebase" /></a></p>
  <p><a href="https://i.stack.imgur.com/BhKDX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BhKDX.png" alt="like collection-firebase" /></a></p>
  <p>the code:</p>
  <pre><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&lt;String&gt; users) {
                  Log.e(&quot;users&quot;, users.size()+&quot;&quot; );
              }
          });
  
          return view;
      }
  
  </code></pre>
  <pre><code>
  private void getUsers(final UserListCallback myCallback) {
  
          FirebaseDatabase.getInstance().getReference().child(&quot;users&quot;).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                  Log.e(&quot;si&quot;, &quot;hff&quot; + &quot;&quot;);
                  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&lt;String&gt; users) {
                              Log.e(&quot;posts&quot;, posts.size()+&quot;&quot; );
                              // System.out.println(&quot;Loaded &quot;+&quot; contacts&quot;);
                          }
                      });
                  Log.e(&quot;si&quot;, &quot;fff&quot; + &quot;&quot;);
              }
  
  
  
              @Override
              public void onCancelled(@NonNull DatabaseError error) {
  
              }
  
  
          });
      }
  </code></pre>
  <pre><code>private void getPosts(final PostListCallback myCallback) {
          FirebaseDatabase.getInstance().getReference().child(&quot;Posts&quot;).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  
                  for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                      Log.e(&quot;p&quot;, &quot;pp&quot; + &quot;&quot;);
  
                      Post post = snapshot.getValue(Post.class);
  
                      posts.add(post.getPostid());
                  }
                  myCallback.onCallback(posts);
                  prepareArray(new ArrayCallback() {
                      @Override
                      public void onCallback(ArrayStructure data) {
                          Log.e(&quot;posts&quot;, posts.size()+&quot;&quot; );
                          // System.out.println(&quot;Loaded &quot;+&quot; contacts&quot;);
                      }
                  });
              //    prepareArray(final UserListCallback myCallback);
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError error) {
  
              }
  
  
          });
      }
  </code></pre>
  <pre><code>private void prepareArray(final ArrayCallback myCallback) {
          for(int i=0 ;i&lt;users.size();i++){
              for(int j=0;j&lt;posts.size();j++){
                  
                  int finalI = i;
                  int finalJ = j;
  
                  FirebaseDatabase.getInstance().getReference().child(&quot;Likes&quot;).addValueEventListener(new ValueEventListener() {
  
                      @Override
                      public void onDataChange(@NonNull DataSnapshot snapshot) {
  
                          if(snapshot.child(posts.get(finalJ)).exists()){
                              Log.e(&quot;j&quot;, finalJ+&quot;: &quot;+posts.get(finalJ) );
  
                              FirebaseDatabase.getInstance().getReference().child(&quot;Likes&quot;).child(posts.get(finalJ)).addValueEventListener(new ValueEventListener() {
                                          @Override
                                          public void onDataChange (@NonNull DataSnapshot d4snapshot){
  
                                              if (d4snapshot.child(users.get(finalI)).exists()) {
                                                  Log.e(&quot;i&quot;, finalI+&quot;: &quot;+users.get(finalI) );
  
                                                  Data.add(finalI, finalJ, 1.0);
                                              } else {
                                                  Log.e(&quot;i&quot;, finalI+&quot;: &quot;+users.get(finalI) );
  
                                                  Data.add(finalI,
                                                          finalJ,
                                                          0.0);
                                              }
                                              //   Log.e(&quot;post&quot;, j+ &quot;&quot;);
  
                                          }
  
  
                                          @Override
                                          public void onCancelled (@NonNull DatabaseError error){
  
                                          }
                                      });
                          }
                          else {
                              Data.add(finalI,
                                      finalJ,
                                      0.0);
                              Log.e(&quot;j&quot;, finalJ+&quot;: &quot;+posts.get(finalJ) );
  
                          }
                   //
  
                      }
  
  
                      @Override
                      public void onCancelled(@NonNull DatabaseError error) {
  
                      }
                  });
              }
          }
          myCallback.onCallback(Data);
          data= Data.toArray();
          for(int l=0 ;l&lt;data.length;l++){
             // Log.e(&quot;user&quot;, users.size()+&quot;&quot; );
              for(int f=0;f&lt;data[l].length;f++){
                  Log.e(&quot;post&quot;, posts.size()+&quot;&quot; );
                  Log.e(&quot;hi&quot;, data[l][f]+&quot;&quot; );
              }
          }
      }
  
  </code></pre>