You always get the gift ID of the last gift in the list, because you created your buttons using this code:
giftimg =new BitmapField(bmpResized,FOCUSABLE) { protected boolean navigationClick(int status, int time) { Dialog.alert("giftsid "+giftsid); UiApplication.getUiApplication().pushScreen(new SendGift(giftsid)); return true; } };
Your navigationClick() method uses the giftsid variable, which is a constant member variable of your class. You assign this variable in your for loop, so the last value it saves is the last value assigned in the loop ( giftsidarr.getString(totalgifts) ).
Although you declare the navigationClick() method in a loop where giftsid has many different meanings, the navigationClick() method uses the giftsid value when it works . The last value.
There are many ways to fix this. You can use a separate constant value in your loop:
final String nextGiftsId = giftsid; giftimg =new BitmapField(bmpResized,FOCUSABLE) { protected boolean navigationClick(int status, int time) { Dialog.alert("nextGiftsId= "+nextGiftsId); UiApplication.getUiApplication().pushScreen(new SendGift(nextGiftsId)); return true; } };
Or, as Signare suggested, attach a cookie to each button that identifies its corresponding gift:
giftimg =new BitmapField(bmpResized,FOCUSABLE) { protected boolean navigationClick(int status, int time) { String giftId = (String)getCookie();
source share