Question

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;
Was it helpful?

Solution

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);
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top