views:

33

answers:

0

Hi,

I want to know how to populate a list field with the data that is retrieved from Json url. I have Json data in the url like so...

[{"CategoryID":13,"CategoryName":"Desktops"},{"CategoryID":14,"CategoryName":"PC"},{"CategoryID":15,"CategoryName":"Mac"}]

I want to display category names into a listfield. Plzzzz help me.It will be very helpful if i get some sample code or a tutorial of how to do it... I am in desperate need of it...

Edit:

This is what i did to get the list. But i am getting an empty list... Can anyone point out what wrong i m doing???

private ListField shoppingListField;
VerticalFieldManager vfm;

private Vector shoppingList = new Vector(10);



public ShoppingMainScreen() {
    super(Bitmap.getBitmapResource("SplashScreen.jpg"), 5);

    HttpRequestDispatcher dispatcher = new HttpRequestDispatcher("http://sampleJsonurl.txt",
            "GET", this);
    dispatcher.start();

     this.setTitle("Shopping");

    shoppingListField = new ListField(shoppingList.size());
    shoppingListField.setCallback(this);
    this.add(shoppingListField);

}

public void requestFailed(final String message) {
    // TODO Auto-generated method stub

    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
        Dialog.alert("Request failed. Reason: " + message);
        }
        });

}

public void requestSucceeded(byte[] result, String contentType)  {

    String strResult = new String(result);
    synchronized (UiApplication.getEventLock()) {

        getShoppingList gShops=new getShoppingList(strResult);
        vfm=gShops.showShops();


    }

}

class getShoppingList
{

    private String strShops;
    public int totalShops;
    public JSONArray jsArrShops;
    private VerticalFieldManager vfmShops;
    public getShoppingList(String shops)
    {
        strShops=shops;
        vfmShops=new VerticalFieldManager();
    }
    public VerticalFieldManager showShops(){
        try {

            jsArrShops=new JSONArray(strShops);
            totalShops= jsArrShops.length();
            for(int i=0;i<totalShops;i++){
                 String strShop=jsArrShops.get(i).toString();
                 JSONObject joShop=new JSONObject(strShop);
                 String shopName= joShop.get("CategoryName").toString();
                 shoppingList.addElement(shopName);
            } 

        }
            catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return vfmShops;

        }

}


public void drawListRow(ListField listField, Graphics graphics, int index,
        int y, int width) {
    // TODO Auto-generated method stub

    getShoppingList shopsList = (getShoppingList) this.get(listField, index);
    int drawColor = Color.BLACK;
    graphics.setColor(drawColor);
    // graphics.drawText(shopsList.showShops() , 0, y, 0, );

}

public Object get(ListField listField, int index) {
    // TODO Auto-generated method stub
    return shoppingList.elementAt(index);
}

public int getPreferredWidth(ListField listField) {
    // TODO Auto-generated method stub
     return Display.getWidth();
}

public int indexOfList(ListField listField, String prefix, int start) {
    // TODO Auto-generated method stub
     return start;
}