I am doing the Android scanning barcode application and I have an object ArrayList ArrayList<Item> itemArrayList for storing all the data. However, I want to check every received for item barcode is unique so I check the ArrayList if it is having the rackname and itemname from the saveItem method.
Below is saveItem method:
private void saveItem(final String rackName, final String itemName, String date, String time) {
Item saveItem = new Item(username, rackName, itemName, date, time);
if (itemArrayList.size() != 0) {
synchronized (itemArrayList) {
for (Iterator<Item> iterator = itemArrayList.iterator(); iterator.hasNext(); ) {
Item item = iterator.next();
if (rackName.equals(item.getRackNumber()) && itemName.equals(item.getItemNumber())) {
dialogDuplicate(itemName); //dialog for duplicate item
} else {
itemArrayList.add(saveItem);
itemAdapter.notifyDataSetChanged();
}
}
}
} else {
Toast.makeText(getApplicationContext(), "Saved for the 1st item", Toast.LENGTH_SHORT).show();
itemArrayList.add(saveItem);
itemAdapter.notifyDataSetChanged();
}
}
But this method throw java.util.ConcurrentModificationException which occurred from Item item = iterator.next() which I dont know why it would occur. What did I do wrong?
saveItemfragment.