Im having a hard time understanding what is happing with my logic. My goal is to add objects to an arraylist if a string on the object is not already present on current arraylist im making.
So to explain my code below/my intentions..
while I add to rowItems check for versionId that already has been added to rowItems and if so append the name of the current object.
Here is what I have so far:
rowItems = new ArrayList<>();
for (int i = 0; i < SearchResultsHolder.results.size(); i++) {
SearchRowItem item = new SearchRowItem(SearchResultsHolder.results.get(i).get("LAST_NAME").toString() + ", " + SearchResultsHolder.results.get(i).get("FIRST_NAME").toString(),
SearchResultsHolder.results.get(i).get("BUSINESS_NAME").toString(),
SearchResultsHolder.results.get(i).get("LOB").toString(),
SearchResultsHolder.results.get(i).get("ID_NUMBER").toString(),
cleanCancelled(SearchResultsHolder.results.get(i).get("STATUS").toString()),
SearchResultsHolder.results.get(i).get("EFF_DATE").toString(),
SearchResultsHolder.results.get(i).get("EXP_DATE").toString(),
SearchResultsHolder.results.get(i).get("VERSION_ID").toString());
if (i == 0){
rowItems.add(item);//add first object by default
}
else {
for (int p = 0; p < rowItems.size(); p++) {
// if the version id is equal to any of the others already in the rowItems array appent the name to the object already in the array
if (item.getVersionId().toString().equals(rowItems.get(p).getVersionId().toString()))
{
item.setName(item.getName().toString() + "\n" + rowItems.get(p).getName().toString());
break;
} else {
rowItems.add(item);
}
}
}
}
System.out.println("row item" + rowItems.toString());
SearchResults holder has a layout like this:
[
{
VERSION_ID=50,
STATUS=Active,
ID_NUMBER=1234,
FIRST_NAME=JOHN,
LAST_NAME=DOE
},
{
VERSION_ID=50,
STATUS=Active,
ID_NUMBER=1234,
FIRST_NAME=JANE,
LAST_NAME=DOE
},
{
VERSION_ID=100,
STATUS=Dead,
ID_NUMBER=1234,
FIRST_NAME=JOHN,
LAST_NAME=DOE
},
{
VERSION_ID=100,
STATUS=Dead,
ID_NUMBER=1234,
FIRST_NAME=JANE,
LAST_NAME=DOE
},
]
And what Im trying to achieve with the objects in my class: (rowitems arraylist)
[
{
VERSION_ID=50,
STATUS=Active,
ID_NUMBER=1234,
NAME=JANE,DOE
JOHN,DOE
},
{
VERSION_ID=100,
STATUS=Dead,
ID_NUMBER=1234,
NAME=JANE,DOE
JOHN,DOE
},
]
What I get :
[
{
VERSION_ID=50,
STATUS=Dead,
ID_NUMBER=1234,
NAME=JOHN,DOE
},
{
VERSION_ID=100,
STATUS=Active,
ID_NUMBER=1234,
NAME=JOHN,DOE
}
]