0

I want to create a chat app with android studio and when I want to display users in my app, app crashed and my code is below:

private void readChats()
{
    mUsers = new ArrayList<>();

    reference = FirebaseDatabase.getInstance().getReference("Users");

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

            for (DataSnapshot snapshot : dataSnapshot.getChildren())
            {
                User user = snapshot.getValue(User.class);

                for (String id : userList){
                    assert user != null;
                    if (user.getId().equals(id)) {
                        if (mUsers.size() != 0) {
                            for (User user1 : mUsers) {
                                if (!user.getId().equals(user1.getId())) {
                                    mUsers.add(user);
                                }
                            }
                        }else {
                            mUsers.add(user);
                        }
                    }
                }
            }
            userAdapter = new UserAdapter(getContext(), mUsers);
            recyclerView.setAdapter(userAdapter);

        }

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

        }
    });
}

and my error is below:

    java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at com.example.mahdi.chatapp.Fragments.ChatsFragment$2.onDataChange(ChatsFragment.java:101)

and error from this line:

for (User user1 : mUsers)

I can't fix this error please help me:

0

4 Answers 4

2

You can't change this list ("mUsers") in loop, because loop use count items for limits. If you make temp variable, or use this code:

for (User user : new ArrayList< User >(mUsers)) {
  if (!user.getId().equals(user1.getId())) {
 }

}

I hope it helpful

Sign up to request clarification or add additional context in comments.

Comments

1

If you use plain for loop instead of enhanced for loop, your problem is resolved.

Comments

1
if (mUsers.size() != 0) {
    for (User user1 : mUsers) {
        if (!user.getId().equals(user1.getId())) {
            mUsers.add(user);
        }
    }
}else {
    mUsers.add(user);
}

Aside from the ConcurrentModificationException (which comes from the mUsers.add(user) inside the loop over mUsers), I don't think this is the logic you intend. This would add user to the list N times, where N is the number of users in the list with differing IDs.

I suspect you might want something like:

if (mUsers.stream().noneMatch(u -> user.getId().equals(u.getId())) {
  mUsers.add(user);
}

which adds user once, if no other user with that ID is present.

You might also consider using a Map<String, User>, where the key is the user's ID. Then you could use:

map.computeIfAbsent(user.getId(), k -> user);

Comments

0

I was too facing this issue. Better you can use the below code snippet which will make sure that only one user has been added in the ChatFragment.

        reference=FirebaseDatabase.getInstance().getReference("Users");

        reference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();

                for(DataSnapshot snapshot:dataSnapshot.getChildren()){


                    User user=snapshot.getValue(User.class);

                    //Display 1 user from chat
                    for (String id:usersList){
                        assert user != null;
                        if(user.getId().equals(id)){
                            if(mUsers.size()!=0){
                                int flag=0;
                               for(User u : mUsers) {
                                   if (user.getId().equals(u.getId())) {
                                       flag = 1;
                                       break;
                                   }
                               }
                               if(flag==0)
                                   mUsers.add(user);
                            }else{

                                mUsers.add(user);
                            }
                        }
                    }
                }

                userAdapter=new UserAdapter(getContext(),mUsers,true);
                recyclerView.setAdapter(userAdapter);

Thank You

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.