Android - Returning a position of an item in an ArrayList that appears multiple times -
i have 3 arraylists, 2 strings , last 1 integer.
the first arraylist contains specific variant (variantarray) product flavor variant of cola, second 1 contains unit (unitarray) contains unit size (80oz, 500ml, 1l) of product, , last 1 quantity *(quantityarray).
this class use.
public class currentorderclass { //arraylists private arraylist<string> variantarray = new arraylist<string>(); private arraylist<string> unitarray = new arraylist<string>(); private arraylist<integer> quantityarray = new arraylist<integer>(); //todo arraylist functions public arraylist<string> getunitarray() { return unitarray; } public void setunitarray(arraylist<string> unitarray) { this.unitarray = unitarray; } public void addtounitarray(string unit){ this.unitarray.add(unit); } public arraylist<integer> getquantityarray() { return quantityarray; } public void setquantityarray(arraylist<integer> quantityarray) { this.quantityarray = quantityarray; } public void addtoquantityarray(int quantity){ this.quantityarray.add(quantity); } public arraylist<string> getvariantarray() { return variantarray; } public void setvariantarray(arraylist<string> variantarray) { this.variantarray = variantarray; } public void addtovariantarray(string variantarray){ this.variantarray.add(variantarray); } @override public string tostring() { return "[ product=" + productname + ", variants=" + variants + " , unit=" + unit + " , quantity=" + quantity + "]"; } } i take in user input user chooses variant, unit, , quantity , input stored in respective arraylists.
however, i'm having problem updating arraylists when user inputs variant , unit exists in arraylists different quantity. want not add new entry, update current arraylists in such way when user inputs variant , unit different quantity, i'd update quantity.
the first thing tried see if input exists in arraylist using indexof(userinputvariant) , check if matches indexof(userinputunit), mean user input repeated already. however, don't think indexof runs on logic , value returns value found first instance of userinputvariant string.
the second attempt tried using each loop, however, i'm once again having hard time returning index want properly.
i instantiate object of currentorderclass named currentorder , prepopulated first 10 elements of variantarray "grape"
after that, tried if-else statement:
if( (currentorder.getvariantarray().indexof(product.getvariant()[variantposition]) == currentorder.getunitarray().indexof(product.getunit()[position]) however, mentioned above, indexof returns int first found string told find, don't think checks if exists after position.
the second code tried use for-each , return position there, again, returned position = 0.
for( string grape : currentorder.getvariantarray() ){ log.d("angelo", "retrived element: " + grape + " @ position = " + currentorder.getvariantarray().indexof(grape)); } i'm thinking of running for-each loop inside for-each loop don't know how make return proper position want.
does have idea on how make work properly? need return position of item appears multiple times in variantarray "cross-referencing" unitarray. thanks.
one loop enough in case.
this thing like:
isexist = false; for(index = 0; index<variantarray.size(); index++){ if(variantarray.get(index) == userchoicevariant && unitarray.get(index) == userchoiceunit){ //update quantity isexist = true; } } if(!isexist){ //insert order }
Comments
Post a Comment