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
Highlighted
Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd
Accepted Solution

show json data in custom List Field

[ Edited ]

hi, i have done json parsing and i have created custom List field. Now i want to display that parsed data in my Custom List field. i'll post my parsed data from json and here is code for my custom List field
parsed data. 
i have THREE Strings from json and i want to show tittle content and date in List filed. i'll post screen shot of my List.

JSONArray jsnarry = new JSONArray(responce);
			System.out.println("\n--length----- "+jsnarry.length());
			//System.out.println("....................................................=");
			for (int i = 0; i < jsnarry.length(); i++){
				
				JSONArray inerarray = jsnarry.getJSONArray(i);
				    //System.out.println("\n-inerarray-values----- "+inerarray.getString(i1));
				    String TITTLE = inerarray.getString(1);
				    String CONTENT = inerarray.getString(2);
				    String DATE = inerarray.getString(3);
				    
System.out.println("TITTLE= "+TITTLE); System.out.println("CONTENT= "+CONTENT); System.out.println("DATE= "+DATE); }

 output  

[0.0] --length----- 2
[0.0] 
[0.0] -innerarray-length----- 6

[0.0] TITTLE= BJP State President Sanjay Tandon's visit to Amita Shukla's Home
[0.0] CONTENT=  BJP President Chandigarh Sanjay Tandon at Amita Shukla's Home
[0.0] DATE= 2013-01-04
[0.0] ................................................
[0.0] TITTLE= Sanjay Tandon at mahasamadhi of Satya Shri Sai baba.
[0.0] CONTENT= BJP Chandigarh President, Sanjay Tandon mahasmadhi of Sri Satya Sai Baba.(Andhra Pradesh)
[0.0] DATE= 2013-01-13

 and my Custom List field

		 super(NO_VERTICAL_SCROLL);
		   
		 String TITTLE="TITTLE";
		 String CONTENT = "CONTENT";
		 String DATE = "DATE";
	        
		 v.addElement(new ListRander(listThumb, TITTLE, CONTENT,DATE, navBar));
		 
		 myListView = new CustomListField(v){
			 
			 protected boolean navigationClick(int status, int time) {
				 //Dialog.alert(" time in milisec :" + time);
				 return true;
			 }
		 };

 CustomListField.java

public class CustomListField extends ListField implements ListFieldCallback {

    private Vector _listData;
    private int _MAX_ROW_HEIGHT = 100;
    
    public CustomListField (Vector data) {

        _listData = data;
        setSize(_listData.size());
        setSearchable(true);
        setCallback(this);
        setRowHeight(_MAX_ROW_HEIGHT);

    }

    public int moveFocus (int amount, int status, int time) {

        this.invalidate(this.getSelectedIndex());
        return super.moveFocus(amount, status, time);

    }

    public void onFocus (int direction) {

        super.onFocus(direction);

    }

    protected void onUnFocus () {

        this.invalidate(this.getSelectedIndex());

    }

    public void refresh () {

        this.getManager().invalidate();

    }

    public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w) {

        ListRander listRander = (ListRander)_listData.elementAt(index);
        graphics.setGlobalAlpha(255);
        graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN, 24));
        final int margin =5;
        
        final Bitmap thumb= listRander.getListThumb();
        final String listHeading = listRander.getListTitle();
        final String listDesc= listRander.getListDesc();
        final String listDesc2= listRander.getListDesc2();
        final Bitmap nevBar = listRander.getNavBar();
        
        //list border
        graphics.setColor(Color.BLACK);
        graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT);
      
        graphics.drawBitmap(margin, y+margin+10, thumb.getWidth(), thumb.getHeight(), thumb, 0, 0);

       
        graphics.drawText(listHeading, 3*margin+thumb.getWidth(), y+margin);
        graphics.setColor(Color.BLACK);

      
        graphics.drawText(listDesc, 3*margin+thumb.getWidth(), y+ margin+30);
        graphics.drawText(listDesc2, 3*margin+thumb.getWidth(), y+ margin+60);

    }

    public Object get(ListField listField, int index) {

        String rowString = (String) _listData.elementAt(index);
        return rowString;

    }

    public int indexOfList (ListField listField, String prefix, int start) {

        for (Enumeration e = _listData.elements(); e.hasMoreElements(); ) {

            String rowString = (String) e.nextElement();
            if (rowString.startsWith(prefix)) {

                return _listData.indexOf(rowString);

            }

        }

        return 0;

    }

    public int getPreferredWidth(ListField listField) {

        return 3 * listField.getRowHeight();

    }

}

Listrander.java

public class ListRander {

private Bitmap listThumb= null;
private Bitmap navBar = null;
private String listTitle=null;
private String listDesc= null;
private String listDesc2= null;

public ListRander(Bitmap listThumb,String listTitle, String listDesc, String listDesc2, Bitmap navBar) {
this.listDesc = listDesc;
this.listDesc2 = listDesc2;
this.listThumb = listThumb;
this.listTitle = listTitle;
this.navBar = navBar;
}
public Bitmap getListThumb() {
return listThumb;
}
public void setListThumb(Bitmap listThumb) {
this.listThumb = listThumb;
}
public Bitmap getNavBar() {
return navBar;
}
public void setNavBar(Bitmap navBar) {
this.navBar = navBar;
}
public String getListTitle() {
return listTitle;
}
public void setListTitle(String listTitle) {
this.listTitle = listTitle;
}
public String getListDesc() {
return listDesc;
}
public void setListDesc(String listDesc) {
this.listDesc = listDesc;
}
public String getListDesc2() {
return listDesc2;
}
public void setListDesc2(String listDesc2) {
this.listDesc2 = listDesc2;
}
}

 

 

9700.png

Super Contributor
Posts: 425
Registered: ‎01-02-2013
My Device: BOLD 9900
My Carrier: sdvsdvsd

Re: show json data in custom List Field

i think i have created every thing but not able make logic for show data in list field 
if you look in the JSON i 'm getting the ARRAY having two length 2 which have data for my rows of list.

data for my first row in json array in first index of JsonArray

[0.0] TITTLE= BJP State President Sanjay Tandon's visit to Amita Shukla's Home
[0.0] CONTENT=  BJP President Chandigarh Sanjay Tandon at Amita Shukla's Home
[0.0] DATE= 2013-01-04

 data for my second row in json array in second index of JsonArray below

[0.0] TITTLE= Sanjay Tandon at mahasamadhi of Satya Shri Sai baba.
[0.0] CONTENT= BJP Chandigarh President, Sanjay Tandon mahasmadhi of Sri Satya Sai Baba.(Andhra Pradesh)
[0.0] DATE= 2013-01-13


how can show them in custom List ?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: show json data in custom List Field

[ Edited ]

You seem to have two issues here and are confusing them.  You need to break this problem up into two parts

1) Extracting the data from the input and creating the Objects you want to display

2) Displaying in a list form, a series of Objects.

 

Let us get the first one sorted first. 

 

I'm going to suggest what to do here, but in practice, you would actually think about this yourself as part of the design phase of your application.  You should do this, not me, because then you will have all the information available.  At present I have only what you have told us, which is not a lot.  So perhaps what I am telling you is not correct for your application.  Only you can decide that.  And, being blunt here, you should have decided this before you started coding.  Doing this as you are going could lead you down the wrong path.  You need to think of your application as a house - you, as the architect, need to design all the rooms and how they will be constructed, before you start building the house.  You haven't, so we are constructing the rooms on the fly.  Who knows if they will fit the house?

 

In this case, I think you should create an new data Object that represents each of the items in the Inner array.  let us call that object

NewsItem

This object will have attributes, like its title, the content, the date, the image associated and so on, each of which will have get and set methods.  So as you are processing each inner item you will extract the associated entry and update the Object. 

 

Now when you have finished processing the inner loop, you have a complete

NewsItem

Object, so you will add this to a collection, an Array of NewsItem objects, call this _newsItems.  You will create this at the start - you know how many entries it needs because that is the number of entries in your outer array. 

 

So before you start processing the JSON, create your array and set the 'index' to 0.

Once you have created your Newsitem, add this to the array at position 'index' and increment 'index'. 

 

And once you have parsed all the JSON, you will have a complete array.  That is part 1 completed!

 

And note in your drawListRow, you are given an index - that is the index into your _newsItems arrays.  So you can easily find which entry to display and display it appropriately.  But that is part 2 and is a separate question.