Hey so I'm working on small project where I use google vision api, the point is to read barcodes and list them. I want to be able to read a barcode multiple times and just increase the count of the same 'barcodeItem' object that I have added in my array of barcodeItem objects.
if(currentBarcode != null){
boolean exists = false;
BarcodeItem barcodeItem = new BarcodeItem(currentBarcode);
for (BarcodeItem item : MainActivity.barcodesList){
if(item.barcode == barcodeItem.barcode){
item.itemCount++;
exists = true;
} else {
//do nothing to item
}
}
if(exists == false){
MainActivity.barcodesList.add(barcodeItem);
}
currentBarcode = null;
} else { //do nothing
}
I've also tried using contains
. Right now the code doesn't actually increase the count of the object, it always adds a new object to the list, is there a way I could check the list of objects for that same barcode and then increase the count accordingly?
EDIT: Okay, thanks for the answers, I actually managed to fix it. Forgot to mention that barcode attribute is type String, and also forgot about the fact that you don't compare Strings with ==
but with equals
instead. Sorry and thank you all for taking the time to help me out.