Blackberry - Clickable BitmapField with various identifiers

I am creating one application in which I receive gift images with an identifier from a web server via JSON. When I click on any image of a gift, it goes to the next page, where all information about this image is displayed (get information about the image with its identifier from the web server via JSON).

The problem is this: when I click on any gift image on the page to see its corresponding information, it gets the last gift image identifier every time, I want, when I click on any image, it gets the specific image identifier that I click. How is this possible ??

Screenshot at: http://ugo.offroadstudios.com/gifts.png

Here is a sample code:

public class Gifts extends MainScreen { String giftsid; BitmapField giftimg; public Gifts(){ setTitle("Gift Store"); creategifts(); } public void creategifts() { //Link URL String strURL = "http://ugo.offroadstudios.com/api/frndgift/?loginusername=adil;deviceside=true"; webConnection wb = new webConnection(); String res = wb.getJson(strURL); try { JSONObject object = new JSONObject(res); if(object.getString("status") == "error") { Dialog.alert("Invalid "+object.getString("status")); } else { int totalgifts; totalgifts = object.getInt("totalgifts"); Bitmap listThumb; JSONArray imagearr; JSONArray giftsidarr; String imgname; Bitmap bmpResized; for(int i=0; i < totalgifts; i++){ imagearr = object.getJSONArray("gifts_image"); imgname = imagearr.getString(i); giftsidarr = object.getJSONArray("gifts_id"); giftsid = giftsidarr.getString(i); listThumb = getImage.getImageFromUrl("http://ugo.offroadstudios.com/wp-content/plugins/bp-gifts-rebirth/includes/images/"+imgname+";deviceside=true"); bmpResized = GPATools.ResizeTransparentBitmap(listThumb, 80, 80, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT); giftimg =new BitmapField(bmpResized,FOCUSABLE) { protected boolean navigationClick(int status, int time) { Dialog.alert("giftsid "+giftsid); UiApplication.getUiApplication().pushScreen(new SendGift(giftsid)); return true; } }; add(giftimg); } } } catch (JSONException e) { System.out.println("EX is "+e); e.printStackTrace(); } } } 
+4
source share
2 answers

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(); // read gift id from the cookie Dialog.alert("giftId= "+giftId); UiApplication.getUiApplication().pushScreen(new SendGift(giftId)); return true; } }; giftimg.setCookie(giftsid); // set the cookie after creating the field 
+1
source

Inside the for loop add the following code -

 giftimg[i].setChangeListener(this); 

Then -

 public void fieldChanged(Field field, int context) { for(int i=0;i<totalgifts;i++) { if(field == giftimg[i]) { // you can trigger your event } } 

EDIT: -

 giftimg[i].setChangeListener(listener); listener = new FieldChangeListener() { public void fieldChanged(Field field, int context) { if ( field instanceof BitmapField ) { for(int i=0;i<totalgifts;i++) { if ( field == giftimg[i] ) { // you can trigger your event } } } } }; 
0
source

All Articles