0

Every time I scan a pctCode, I add item to my ArrayList. Unfortunately I can get double items when the user scans it twice.

With Google I found:

Solution A (before add, look at list if already exist):

if (ListOrdres.contains(OrdreItem))             
    ListOrdres.add(OrdreItem);

Solution B (use HashSet):

ListOrdres.add(OrdreItem);

HashSet<Pers_Ordre> hs = new HashSet();
hs.addAll(ListOrdres);
ListOrdres.clear();
ListOrdres.addAll(hs);  

Neither Solution A nor B work, I still get double items.

Here is my code:

public class MainActivity extends Activity implements OnClickListener
{
    ...
    ArrayList<Pers_Ordre> ListOrdres = new ArrayList<Pers_Ordre>();
    ....

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_Scan:
            {
                IntentIntegrator scanIntegrator = new IntentIntegrator(this);
                scanIntegrator.initiateScan();
                ........

                public void onActivityResult ( int requestCode, int resultCode, Intent intent)
                {
                    .......

                    //retrieve scan result
                    IntentResult scanningResult =
                            IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

                    if(scanningResult != null)
                    {
                        //we have a result
                        String scanContent = scanningResult.getContents();
                        if(scanContent != null)
                        {
                            Pers_Ordre OrdreItem = new Pers_Ordre();

                            String[] TempOrdre = scanContent.split("_");
                            OrdreItem.setLe_CodeClient(TempOrdre[0]);
                            OrdreItem.setLe_CodeDest(TempOrdre[1]);
                            OrdreItem.setLe_NoOrdre(TempOrdre[2]);
                            OrdreItem.setLe_Ordre_1(TempOrdre[3]);
                            OrdreItem.setLe_Ordre_2(TempOrdre[4]);
                            OrdreItem.setLe_Date(LedateFormat.format(Ledate));
                            OrdreItem.setLe_GPS(TempAddr);

                            ListOrdres.add(OrdreItem);

                            HashSet<Pers_Ordre> hs = new HashSet();
                            hs.addAll(ListOrdres);
                            ListOrdres.clear();
                            ListOrdres.addAll(hs);
                            m_adapter = new CustomListAdapter(this, ListOrdres);
                            m_adapter.notifyDataSetChanged();
                            lv_Ordre.setAdapter(m_adapter);
                        }
                    }
                }
            }
        }
    }
}

here is my Persistence Ordre Code:

public class Pers_Ordre {

    private String Le_CodeClient;
    private String Le_CodeDest;
    private String Le_NoOrdre;
    private String Le_Ordre_1;
    private String Le_Ordre_2;
    private String Le_Date;
    private String Le_GPS;

    public String getLe_Date() {
        return Le_Date;
    }
    public void setLe_Date(String le_Date) {
        Le_Date = le_Date;
    }

    public String getLe_GPS() {
        return Le_GPS;
    }
    public void setLe_GPS(String le_GPS) {
        Le_GPS = le_GPS;
    }

    public String getLe_CodeClient() {
        return Le_CodeClient;
    }
    public void setLe_CodeClient(String le_CodeClient) {
        Le_CodeClient = le_CodeClient;
    }
    public String getLe_CodeDest() {
        return Le_CodeDest;
    }
    public void setLe_CodeDest(String le_CodeDest) {
        Le_CodeDest = le_CodeDest;
    }
    public String getLe_NoOrdre() {
        return Le_NoOrdre;
    }
    public void setLe_NoOrdre(String le_NoOrdre) {
        Le_NoOrdre = le_NoOrdre;
    }
    public String getLe_Ordre_1() {
        return Le_Ordre_1;
    }
    public void setLe_Ordre_1(String le_Ordre_1) {
        Le_Ordre_1 = le_Ordre_1;
    }
    public String getLe_Ordre_2() {
        return Le_Ordre_2;
    }
    public void setLe_Ordre_2(String le_Ordre_2) {
        Le_Ordre_2 = le_Ordre_2;
    }
mata
  • 361
  • 3
  • 14
user609511
  • 4,091
  • 12
  • 54
  • 86
  • 1
    You condition in first `if` statement is wrong. It adds item only if it already exists in array, which is never (if not filling array elsewhere). Negate condition with **!**. – mata Aug 14 '14 at 12:37

2 Answers2

3

The collection type of choice for this case is a HashSet, but you will need to override the equals and hashCode methods in your Pers_Ordre class. As it is now, whenever you check whether an item already exists, the check is performed based on object identity, and will only return true if the objects are effectively one and the same object.

If you don't want to implement the equals and hashCode methods yourself, you can have your IDE automatically generate them for you. By default, most IDEs will generate an implementation that does the equals comparison by comparing all the objects' fields, and generates a hashCode value also based on all the object's fields. You can then adapt where necessary.


This SO question gives some good pointers for equals and hashCode implementations: What issues should be considered when overriding equals and hashCode in Java?

This tutorial provides a basic introduction: http://tutorials.jenkov.com/java-collections/hashcode-equals.html

Community
  • 1
  • 1
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
2

Here is sample...

If you have a List that contains duplicates, you can get the unique entries like this:

List<String> gasList = // create list with duplicates...
Set<String> uniqueGas = new HashSet<String>(gasList);
System.out.println("Unique gas count: " + uniqueGas.size());

NOTE: This HashSet constructor identifies duplicates by invoking the elements' equals() methods.

Bhaskar
  • 911
  • 6
  • 18