2

I add items to a list through a Model class and then add these items to an ArrayList<Object>. However, I always get a size of zero even if the commentsList.size() > 1.

I tried to convert the List to ArrayList and tried adding items. But always the size was 0 on ArrayList<Object>.

ArrayList<Comments> commentsList = new ArrayList<>(Arrays.asList(new Comments("username", "time", "date")));

Here are the functions that I am using.

private ArrayList<Object> getObject() {

    if (getComments() != null && getComments().size()>=1) {
        objects.add(getComments().get(0));
    }

    return objects;
}

public static ArrayList<Comments> getComments() {
    ArrayList<Comments> commentsList = new ArrayList<>();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            commentsList.clear();

            for (DataSnapshot shot : snapshot1.getChildren()) {
                 Comments comments = shot.getValue(Comments.class);
                 commentsList.add(comments);                          
           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

    return commentsList;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
just aguy
  • 47
  • 8
  • There's nothing special about adding a list to an ArrayList. If you add a list to the ArrayList, it will increase in size. If that doesn't seem to be the case, you're either clearing it before you're checking it, or you aren't adding anything. Please fix your formatting and show a [mcve]. – Carcigenicate Jul 10 '19 at 21:41

1 Answers1

0

In the getComments function, you are doing an Asynchronous operation which is retrieving the data from your firebase database. Hence you do not actually have anything in your commentsList. The function is just initializing a new ArrayList with zero elements, of course, and creating a ValueEventListener which is waiting for the data to be received in your application. However, this operation is asynchronous (running in a separate thread) and hence after creating the ValueEventListener, the function is returning immediately with the empty list. Thus you are getting an empty list as well when you are trying to build the ArrayList<Object> in your getObject function.

I would suggest writing another function which will be invoked when the onDataChange function is called after receiving the data from your firebase database. For example, write a function in the same class as the following.

void nowCreateArrayListOfObjects() {
    // TODO: Call the getObject function here
}

Now call this function from your onDataChange function as follows.

public static ArrayList<Comments> getComments() {
    ArrayList<Comments> commentsList = new ArrayList<>();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            commentsList.clear();

            for (DataSnapshot shot : snapshot1.getChildren()) {
                Comments comments = shot.getValue(Comments.class);
                commentsList.add(comments);
            }

            // Invoke the function here to get the values now
            nowCreateArrayListOfObjects();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

    return commentsList;
}

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 1
    Although this doesnt work with static, I adapted the idea and made the classes private voids and everything is smooth now ! – just aguy Jul 12 '19 at 15:35