Java - How to navigate through object array with buttons -
i'm trying implement "previous" , "next" button. issue cannot figure out how navigate through object array buttons , display on fields.
to clarify button iterate through object array , display fields according pointer each click.
an example of want do:
button click
display myproduct @ pointer 0
button click
display myproduct @ pointer 1
button click
display myproduct @ pointer 2
so far i've managed this:
product[] myproduct = new product[]{ new product("dr.pepper", 5, 1.00), new product("coca cola", 6, 1.00), new product("fanta", 7, 1.00), new product("sprite", 8, 1.00), new product("redbull", 9, 1.00), new product("orange juice", 10, 1.00) }; if(source.equals(nextbutton)){ getnamefield.settext(myproduct[++pointer].getname()); getstocklevelfield.settext(string.valueof(myproduct[++pointer].getstocklevel())); getpricefield.settext(string.valueof(myproduct[++pointer].getprice())); } }
link examples:
you can see button works , iterates 1. want can iterate through of objects 1 one.
thanks in advance.
define new integer this:
int pointer = 0;
this integer stores current viewed index of array.so in button click this:
a)for next: myproduct[++pointer].getname();
b)for previous: myproduct[--pointer].getname();
note:you must check pointer on each click less 0 previous , greater lenght of myproduct.
Comments
Post a Comment