views:

26

answers:

1

Hi,

I am retrieving a list of objects from json url and displaying it like a list by adding a label field and a seperator. Now, i want to make each label clickable so that each label redirects to a seperate url. On clicking the label a seperate screen must open with the json data of the corresponding url. So, can anyone tell me how to achieve this. I wil be really grateful if i get some sample code of how to do it... Here's some sample code that i have done...

public VerticalFieldManager showShoppingList(){
        try {
            jsArrShpList=new JSONArray(strShopping);
            totalList= jsArrShpList.length();
            for(int i=0;i<totalList;i++){
                 String strAlert=jsArrShpList.get(i).toString();
                 JSONObject joAlert=new JSONObject(strAlert);
                 String shoppingList = joAlert.get("CategoryName").toString();
                 LabelField shops  = new LabelField(shoppingList);
                 VerticalFieldManager vfmListRow=new VerticalFieldManager();
                 vfmListRow.add(shops);
                 vfmListRow.add(new SeparatorField());
                 vfmShpList.add(vfmListRow);

            }

            return vfmShpList;
+1  A: 

Instead of using a bunch of LabelFields which don't normally accept focus or click events, why not use a ListField on the screen? That seems to be more of what you're looking for.

If you do want to go with the LabelField approach, you'll need to do a few things. First, when you create your LabelField, use the Field.FOCUSABLE style so that it will accept focus:

LabelField shops  = new LabelField(shoppingList, Field.FOCUSABLE);

Now, since the LabelField field won't call into a change listener if you set one up, you'll need to listen for click and key events in it's parent manager. Since these click or key events can be for any field in the manager, you need to check which field is in focus when the event occurs, and run any appropriate handler based on the field in focus.

Code example:

VerticalFieldManager vfmListRow = new VerticalFieldManager() {
    protected boolean navigationClick(int status, int time) {
        Field field = getFieldWithFocus();
        if (field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.navigationClick(status, time);
    }

    protected boolean keyChar(char key, int status, int time) {
        Field field = getFieldWithFocus();
        if (key == Characters.ENTER && field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.keyChar(key, status, time);
    }
};
Marc Novakowski
Can u plz provide an example that uses ListField to display Json data from url...
Rahul Varma
There are plenty of sample apps that ship with the BlackBerry development tools, many of which use the ListField control. As for JSON your best bet is to use a third party library such as org.json.me
Marc Novakowski