I am using an ArrayList (in Android) that contains the values from the Firebase database. Whenever the data is added or deleted, I want the list to be updated. To do this I used ChildEventListener but I am unsure whether this is a proper way of doing it as I face errors sometimes while deleting the elements from the list. There's no problem at all when adding elements to the list. But When I try to delete the last element from the list, I get ArrayIndexOutOfBounds Exception or some other Exception like length=12 index=-1. So, please go through my code and suggest a better way to delete elements:
    public class Join extends AppCompatActivity {
    DatabaseReference databaseReference;
    ListView listView;
    public static ArrayList<String> keysArrayList; 
    //To store the keys from Firebase
    public static ArrayList<String> namesArrayList; 
    //To store the value or name associated with the key
    ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_join);
    databaseReference = FirebaseDatabase.getInstance().getReference().child(“Queue Codes”);
    listView = (ListView) findViewById(R.id.listViewForMember);
    keysArrayList = new ArrayList<>();
    namesArrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<> 
    (this,android.R.layout.activity_list_item,namesArrayList);
    listView.setAdapter(arrayAdapter);
    /* Here I am trying to store Random keys from Firebase in 
    ‘keysArrayList’ and their values in ‘namesArrayList’ and update these 
    lists whenever a value is added or removed from the database*/
     databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String addedKey = dataSnapshot.getKey();
            String addedName = dataSnapshot.getValue(String.class);
            keysArrayList.add(addedKey);               
            namesArrayList.add(addedName);
            arrayAdapter.notifyDataSetChanged();
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            String removedKey = dataSnapshot.getKey();
            int index = keysArrayList.indexOf(removedKey);
            namesArrayList.remove(index);
            keysArrayList.remove(removedKey);
            arrayAdapter.notifyDataSetChanged();
             /* Here I tried to remove the name from the ‘namesArrayList’ by 
             using the index of removed value from ‘keysArrayList’ as both 
             the lists would be of the same size anytime. I can simply 
             delete the name from the ‘namesArrayList’ by reading the 
             removed value from firebase but gets difficult when I use 
             Custom Array Lists which may contain many other objects. So, I 
             decided to delete the name using key index. */
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}