Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
rahul_kalidindi
Posts: 67
Registered: ‎06-30-2010
My Device: Storm 9550
My Carrier: Airtel

Json data into Blackberry Listfields

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...

 

Please use plain text.
Developer
raquibulbari
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: Json data into Blackberry Listfields

use json-me for j2me to do the json parsing in your applicaiton.

http://developerlife.com/tutorials/?p=624

 

after parsing show them in a list field as you want

----------------------
Press like if you find a message helpful
---------
Please use plain text.
Regular Contributor
rahul_kalidindi
Posts: 67
Registered: ‎06-30-2010
My Device: Storm 9550
My Carrier: Airtel

Re: Json data into Blackberry Listfields

I have no problems in parsing json... I have parsed the json and displayed it on a label using the following code...

jsArrAlerts=new JSONArray(strAlerts);
		totalAlerts= jsArrAlerts.length();
		for(int i=0;i<totalAlerts;i++){
			 String strAlert=jsArrAlerts.get(i).toString();
			 JSONObject joAlert=new JSONObject(strAlert);
			String alertText= joAlert.get("alerttext").toString();
			String alertDate=joAlert.get("alertdate").toString();
			VerticalFieldManager vfmRowAlert=new VerticalFieldManager();
			vfmRowAlert.add(new LabelField(alertText));
			vfmRowAlert.add(new SeparatorField());
			vfmAlerts.add(vfmRowAlert);

 

But i m not sure of how to insert this String(for example alertText ) into list field. Can u plz give me a sample which displays json data into list fields...

 

Please use plain text.
Developer
raquibulbari
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: Json data into Blackberry Listfields

Here is a listfield demo

http://developerlife.com/tutorials/?p=898

----------------------
Press like if you find a message helpful
---------
Please use plain text.
Regular Contributor
rahul_kalidindi
Posts: 67
Registered: ‎06-30-2010
My Device: Storm 9550
My Carrier: Airtel

Re: Json data into Blackberry Listfields

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;
}

 

Can anyone point the error and correct me ...

 

Please use plain text.
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009
My Device: Bold

Re: Json data into Blackberry Listfields

what is your size at this line: shoppingListField = new ListField(shoppingList.size());

Is it zero?

 

You probably have to update the ListField after your json data is loaded.

shoppingListField.setSize(newSize);

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Developer
raquibulbari
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: Json data into Blackberry Listfields

Why is this line commented

graphics.drawText(shopsList.showShops() , 0, y, 0, );
----------------------
Press like if you find a message helpful
---------
Please use plain text.
Regular Contributor
rahul_kalidindi
Posts: 67
Registered: ‎06-30-2010
My Device: Storm 9550
My Carrier: Airtel

Re: Json data into Blackberry Listfields

the graphics.drawText() needs a String or a char as the final argument. Since i am using a VerticalFieldManager for showShops() i am a bit confused at the implementation. I'll grateful if you can specify how could i change the code...

Please use plain text.
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009
My Device: Bold

Re: Json data into Blackberry Listfields

Why do you want to use a VerticalFieldManager? Could you not just lay out/draw your Shopping object right there in the drawListRow method?

 

I'm not sure that you can actually place a VericalFieldManager inside a ListField. You could bulid your own ListField / ObjectListField that can take objects like your Shopping objects and draws it based on what type of object your pass in.  That way your list is very flexible as to what you can stick in.

 

For now, I would just do all your layout/drawing of the Shopping object within drawListRow.

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Regular Contributor
rahul_kalidindi
Posts: 67
Registered: ‎06-30-2010
My Device: Storm 9550
My Carrier: Airtel

Re: Json data into Blackberry Listfields

I have removed the Vertical field managar and made it a string. Also added

graphics.drawText(shopsList.showShops() , 0, y, 0, width );

 

 

to the code for drawListRow... Still i couldnt get any result...

 

Please use plain text.