Blackberry - Clickable BitmapField With Different

2019-06-27 17:06发布

i'm creating one application in which i get gift images with id's from web server through JSON. When i click on any gift image, it goes on next page where it shows all information of that image (get image information with its id from web server through JSON).

Problem is: When i click on any gift image on page to see its relevant information, it gets the last gift image id every time, i want when i click on any image, it gets the specific image id which i click. How it is possible??

Screenshot of the page is : http://ugo.offroadstudios.com/gifts.png

Here is 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();
        }
    }
}

标签: blackberry
2条回答
别忘想泡老子
2楼-- · 2019-06-27 17:38

You are always getting the gift id of the last gift in the list because you have created your buttons with 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 used the giftsid variable, which is a persistent member variable of your class. You assign this variable in your for loop, so the final value it keeps is the last value assigned in the loop (giftsidarr.getString(totalgifts)).

Although you declare the navigationClick() method in a loop where the giftsid is many different values, the navigationClick() method uses the value of giftsid when it is run. The last value.

There's many ways to fix it. 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
查看更多
Melony?
3楼-- · 2019-06-27 17:58

Inside your 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 
                        }
                    }
                }
            }
        };
查看更多
登录 后发表回答