本文介绍了黑莓 - 可点击BitmapField用不同的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序中,我得到的礼物与图像的ID从Web服务器通过JSON。当我任何礼物图像上单击,它会在下一个页面上它显示了图像的所有信息(得到其通过JSON从Web服务器ID的图像信息)。

问题是:当我页面上的任何礼物图像上单击以查看其相关信息,它得到的最后一份礼物图片ID每一次,我想,当我的任何图像上单击,它得到具体的图片ID,我点击。它是如何可能?

页面的截图为: http://ugo.offroadstudios.com/gifts.png

下面是示例code:

公共类礼品扩展MainScreen {    串giftsid;
    BitmapField giftimg;    公共礼品(){
        的setTitle(礼品店);
        creategifts();
    }    公共无效creategifts()
    {
        // URL链接
        字符串strURL =htt​​p://ugo.offroadstudios.com/api/frndgift/?loginusername=adil;deviceside=true;
        webConnection WB =新webConnection();
        字符串资源= wb.getJson(strURL);        尝试{
            的JSONObject对象=新的JSONObject(RES);
            如果(object.getString(身份)==错误)
            {
                Dialog.alert(无效的+ object.getString(身份));
            }
            其他
            {
                INT totalgifts;
                totalgifts = object.getInt(totalgifts);
                位图listThumb;
                JSONArray imagearr;
                JSONArray giftsidarr;
                串imgname;
                位图BM presized;                的for(int i = 0; I< totalgifts;我++){
                    imagearr = object.getJSONArray(gifts_image);
                    imgname = imagearr.getString(ⅰ);
                    giftsidarr = object.getJSONArray(gifts_id);
                    giftsid = giftsidarr.getString(ⅰ);                    listThumb = getImage.getImageFromUrl(\"http://ugo.offroadstudios.com/wp-content/plugins/bp-gifts-rebirth/includes/images/\"+imgname+\";deviceside=true\");
                    BM presized = GPATools.ResizeTransparentBitmap(listThumb,80%,80,
                    Bitmap.FILTER_LANCZOS,Bitmap.SCALE_TO_FIT);                    giftimg =新BitmapField(BM presized,可聚焦)
                    {
                        保护布尔navigationClick(INT的地位,诠释时间)
                        {
                            Dialog.alert(giftsid+ giftsid);
                            。UiApplication.getUiApplication()pushScreen(新SendGift(giftsid));
                            返回true;
                        }
                    };
                    加(giftimg);
                }
            }        }
        赶上(JSONException E){
            的System.out.println(EX是+ E);
            e.printStackTrace();
        }
    }
}


解决方案

您始终获得在清单的最后礼物馈赠ID,因为你已经创建你的按钮与此code:

giftimg =新BitmapField(BM presized,可聚焦)
{
    保护布尔navigationClick(INT的地位,诠释时间)
    {
        Dialog.alert(giftsid+ giftsid);
        。UiApplication.getUiApplication()pushScreen(新SendGift(giftsid));
        返回true;
    }
};

navigationClick()方法使用了 giftsid 变量,它是一种持久性的成员变量类的。你在你的循环分配这个变量,所以它使终值处于环路( giftsidarr.getString(totalgifts)分配的最后一个值)。

虽然你的声明的在一个循环中 navigationClick()方法,其中 giftsid 是许多不同的值,在 navigationClick()方法使用 giftsid 的值时,它的运行。最后一个值。

有很多方法可以解决这个问题。您可以在循环使用一个单独的恒定值:

最后弦乐nextGiftsId = giftsid;giftimg =新BitmapField(BM presized,可聚焦)
{
    保护布尔navigationClick(INT的地位,诠释时间)
    {
        Dialog.alert(nextGiftsId =+ nextGiftsId);
        。UiApplication.getUiApplication()pushScreen(新SendGift(nextGiftsId));
        返回true;
    }
};

或者,作为Signare建议,附加的饼干的每个标识及其相应的礼品按钮:

giftimg =新BitmapField(BM presized,可聚焦)
{
    保护布尔navigationClick(INT的地位,诠释时间)
    {
        字符串giftId =(字符串)的getCookie(); //从cookie读取ID礼物
        Dialog.alert(giftId =+ giftId);
        。UiApplication.getUiApplication()pushScreen(新SendGift(giftId));
        返回true;
    }
};
giftimg.setCookie(giftsid); //创建后场设置cookie

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();
        }
    }
}
解决方案

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

这篇关于黑莓 - 可点击BitmapField用不同的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:02