1

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.

3 Answers3

0

In Java when you use the "==" operator on an object you are checking if this is the same object instance. Which is not as you create a new item each time. You have two options:

  1. Have a unique Id for each type and check if it's the same Id.
  2. Override the equals methods in the BarcodeItem and check whatever parameters that makes them the same item.

contains calls for the equals in an object as well so as long as you don't override it in the BarcodeItem object it will return the same as the "==" operator

Raz
  • 8,918
  • 3
  • 27
  • 40
  • what if the barcode is a primitive (int or similar)? there are cases in which == would be appropriate – Stultuske Sep 07 '17 at 07:47
  • @Stultuske, yes. If you are compering int == int than it will check the values. However, if you check objects (also Strings) you need to override the equals method. For example, String is not a primitive type and if you want to compare the text of the Strings then you need to do String.equals("test"). – Raz Sep 07 '17 at 09:08
  • primitives, enums, ... there are other examples as well. sure, the need of equals might very well be the issue here, but hard to say for sure, without knowing the type that is being compared – Stultuske Sep 07 '17 at 09:10
  • @Stultuske, well from his code he his doing new BarcodeItem() so it's an object. – Raz Sep 07 '17 at 09:12
  • I've seen that, but he's not comparing the barcodeItem, but the barcode IN the barcodeItem – Stultuske Sep 07 '17 at 09:14
0

Probably your attribute is not a primitive data so you should also override equals() method of your attribute. Beside this, override the equals() method of BarcodeItem as:

    @Override
public boolean equals(Object obj) {
...
BarcodeItem bi=(BarcodeItem)obj;
if(bi.getYourAttribute.equals(this.yourAttribute))
return true;
}

than use List.contains() method.

ihsan kocak
  • 1,541
  • 1
  • 17
  • 26
0

you can go through this way,

    for (int i = 0; i < MainActivity.barcodesList.size(); i++) {
        if (MainActivity.barcodesList.get(i).getBarcodeNo().equalsIgnoreCase(currentBarcode)) {
            Toast.makeText(this, "Duplicate QR Code : " + currentBarcode, Toast.LENGTH_SHORT).show();
            return;
        }
    }
    BarcodeModel bm = new BarcodeModel();
    bm.setBarcodeNo(currentBarcode);
    MainActivity.barcodesList.add(bm);

your count will be

    MainActivity.barcodesList.size() + 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142