ArrayList<Object> copied from another ArrayList is empty even if there is data

just aguy :

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 :

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!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162565&siteId=1