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
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

ListField(int numRows, long style) problem

Hi,

 

I was trying to create a listField which has n numbers row.

I've used the constructor, ListField(int numRows, long style). But it is giving me ArrayIndexOutOfBoundsException exception.

If I'm using just ListField() or ListField(0, long style), it works fine.

 

Thanks,

DK.

 

 

BlackBerry Development Advisor
Posts: 15,752
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: ListField(int numRows, long style) problem

What values are you using?  What BlackBerry model and handheld software version are you testing on?  You can find this under Options, About on the BlackBerry.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: ListField(int numRows, long style) problem

myList = new ListField(QueryResultScreen.search_no/* static  int */, ListField.FIELD_HCENTER) << exception

myList = new ListField(2, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH) << exception

myList = new ListField(0, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH) <<< This is Ok.

 

Version:

About - BlackBerry Device Simulator -8800
-----------------------------------
BlackBerry Device Simulator 2.6.0.49
Special thanks to the wxWidgets, C++ Boost, and PortAudio projects.
© 1997-2006 Research In Motion Limited

 

Used API is 4.2.1.

 

Device version is Blackberry 8320, V4.2.2.2.180(platform 2.5.0.36).

 

Thanks,

DK.

 

BlackBerry Development Advisor
Posts: 15,752
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: ListField(int numRows, long style) problem

I haven't been able to reproduce this.  Please verify that this is the line of code triggering the exception and provide a larger code snippet if it is.  I also used BlackBerry handheld software version 4.2.2.180.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: ListField(int numRows, long style) problem

Mark,

 

Please see the code sniffet. This is part of the code.

if(QueryResultScreen.search_no == 0) { add(new LabelField ("There is no result. Please try again.",LabelField.NON_FOCUSABLE) { public void paint(Graphics g) { //String str1=""; Font _font = Utils.BBMillBank( 18); g.setColor(0x2dadcc); this.setFont(_font); super.paint(g); } } ); } else { //myList = new ListField()//ListField(QueryResultScreen.search_no, ListField.FIELD_HCENTER) myList = new ListField(0, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH) //myList = new ListField(2, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH) { //Handles moving the focus within this field. public int moveFocus(int amount, int status, int time) { invalidate(getSelectedIndex()); return super.moveFocus(amount,status,time); } //Invoked when this field receives the focus. public void onFocus(int direction) { hasFocus = true; super.onFocus(direction); } //Invoked when a field loses the focus. public void onUnfocus() { hasFocus = false; super.onUnfocus(); invalidate(); } protected void layout(int maxWidth, int maxHeight) { super.layout(Display.getWidth()-20, this.getHeight()); setExtent(Display.getWidth()-20, this.getHeight()); } public void paint(Graphics g) { int[] e_cols = new int[]{EVEN_COLOR,EVEN_COLOR,EVEN_COLOR,EVEN_COLOR}; int[] o_cols = new int[]{ODD_COLOR,ODD_COLOR,ODD_COLOR,ODD_COLOR}; XYRect xy = g.getClippingRect(); if(xy.y < 0) { throw new IllegalStateException("Clipping rectangle is wrong."); } int rowHeight = getRowHeight(); int curSelected; int startLine = xy.y / rowHeight; int y = startLine * rowHeight; int endLine = (xy.y + xy.height - 1) / rowHeight; endLine = Math.min(endLine, getSize() - 1); int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight}; int[] xInds = new int[]{0, getPreferredWidth(), getPreferredWidth(), 0}; Font _font; //If the ListeField has focus determine the selected row. if (hasFocus) { curSelected = getSelectedIndex(); } else { curSelected = -1; } //Setup the data used for drawing. ListFieldCallback callBack = this.getCallback(); for(; startLine <= endLine; ++startLine) //for(startLine=0; startLine <= endLine; ++startLine) { String text =(String)callBack.get(this, startLine); String [] subStr; String phone = null; subStr = Utils.split(text,"|"); //returned phone number and option phone = subStr[4] + ", Distance:" + subStr[5]+ "mi, Rate:" +subStr[6]; if (subStr[subStr.length-1].trim().equals("1")) optionImg = Bitmap.getBitmapResource("option_1.png"); if (subStr[subStr.length-1].trim().equals("2")) optionImg = Bitmap.getBitmapResource("option_2.png"); if (subStr[subStr.length-1].trim().equals("3")) optionImg = Bitmap.getBitmapResource("option_3.png"); if (startLine % 2 == 0 && startLine != curSelected) { g.setColor(LIGHT_TEXT); g.drawShadedFilledPath(xInds, yInds, null, o_cols, null); _font = Utils.BBMillBank(12); g.setColor(DARK_TEXT); g.setFont(_font); g.drawText(" " + subStr[0], 2, y+1); _font = Utils.BBMillBank(Font.PLAIN, 12); g.setColor(0x333333); g.setFont(_font); g.drawText(" " + subStr[1] + " " + subStr[2]+ ", " + subStr[3], 2, y+listHeight+1); _font = Utils.BBMillBank(Font.PLAIN, 12); g.setColor(0xe86e28); g.setFont(_font); g.drawText(" " + phone, 2, y + 2*listHeight+1); //g.setColor(DARK_TEXT); g.drawBitmap(Display.getWidth()-20-optionImg.getWidth(), startLine*optionImg.getHeight(), optionImg.getWidth(), optionImg.getHeight(), optionImg, 0,0); g.setColor(DARK_TEXT); g.drawRect(0, startLine*optionImg.getHeight(), Display.getWidth(), optionImg.getHeight()); g.drawRect(Display.getWidth()-20-optionImg.getWidth(), startLine*optionImg.getHeight(), optionImg.getWidth(), optionImg.getHeight()); System.out.println("SearchScreen ListField paint-1-------text["+text +"] " +", y"+y+ ", rowHeight: " + rowHeight+", startLine:"+ startLine); } else { _font = Utils.BBMillBank(12); g.setColor(DARK_TEXT); g.setFont(_font); g.drawText(" " + subStr[0], 2, y+1); _font = Utils.BBMillBank(Font.PLAIN, 12); g.setColor(0x333333); g.setFont(_font); g.drawText(subStr[1] + " " + subStr[2]+ ", " + subStr[3], 2, y+listHeight+1); _font = Utils.BBMillBank(Font.PLAIN, 12); g.setColor(0xe86e28); g.setFont(_font); g.drawText(" " + phone, 2, y + 2*listHeight+1); g.drawBitmap(Display.getWidth()-20-optionImg.getWidth(), startLine*optionImg.getHeight(), optionImg.getWidth(), optionImg.getHeight(), optionImg, 0,0); g.setColor(DARK_TEXT); g.drawRect(0, startLine*optionImg.getHeight(), Display.getWidth()-20, optionImg.getHeight()); g.drawRect(Display.getWidth()-20-optionImg.getWidth(), startLine*optionImg.getHeight(), optionImg.getWidth(), optionImg.getHeight()); System.out.println("SearchScreen ListField paint-2-------text["+text +"] " + ", y[" + y + "], rowHeight: " + rowHeight+", startLine:"+ startLine); } y += rowHeight; //y=0; yInds[0] = y; yInds[1] = yInds[0]; yInds[2] = y + rowHeight; yInds[3] = yInds[2]; } } }; listHeight =16;// myList.getRowHeight();//DK 092708 myList.setRowHeight(listHeight*3); myCallback = new ListCallback(); myList.setCallback(myCallback); JSONParser jparser = new JSONParser(); String status = jparser.parseJSON(Globals.receivedContent);//parsing received data int size = jparser.row.size(); Enumeration keys = jparser.rowObj.keys(); phoneInfo = new String [jparser.row.size()]; cnt=0; String [] result; String [] subStr; while(keys.hasMoreElements()) { String key = keys.nextElement().toString(); try { phoneInfo[cnt]= jparser.rowObj.getString(key); } catch (JSONException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println(e + "--------------"); Status.show("You received wrong data format, please try again."); } //myList.setFont(_font); for (int i=0; i < QueryResultScreen.bizInfo.length; i++) { subStr = Utils.split(QueryResultScreen.bizInfo[i], "|");// all business info result = Utils.split(phoneInfo[cnt],"|"); //returned phone number and option if (subStr[4].equals(result[0])) { myList.insert(cnt); myCallback.insert(QueryResultScreen.bizInfo[i]+"|"+result[1],cnt); } } cnt++; }

 

BlackBerry Development Advisor
Posts: 15,752
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: ListField(int numRows, long style) problem

From where is this code being called from?  The constructor of a screen or manager or somewhere else?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: ListField(int numRows, long style) problem

This is the part of constructor of a screen.

And is added in VerticalfieldManager.

 

 

BlackBerry Development Advisor
Posts: 15,752
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: ListField(int numRows, long style) problem

I used the first part of your code and no exception was thrown.  The lower portion of the code referrs to variables and classes defined elsewhere, so I wasn't able to test that.  Can you provide a basic sample that reproduces this?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 141
Registered: ‎08-06-2008
My Device: Not Specified

Re: ListField(int numRows, long style) problem

I don't how to provide sample code. It's part of the code.

Basic function is that it is getting the data from the server in JSON format, parse it by JSON.

JSON will return received data with number of the data which is "QueryResultScreen.search_no", this variable defined as  " 

public static int search_no;"  in QueryResultScreen class.

 

 

//myList = new ListField()//ListField(QueryResultScreen.search_no, ListField.FIELD_HCENTER)

 myList = new ListField(0, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH)

//myList = new ListField(2, ListField.FIELD_HCENTER|ListField.USE_ALL_WIDTH)

 

Do you want me to give you whole source code for the screen?

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

Re: ListField(int numRows, long style) problem

Random questions, just trying to help here.  I suspect I am really missing something significant.....

a) Which line in the code shown is throwing the Exception?

b) You say that ListField(0...) works and ListField(2...) throws an exception.  Do you have 2 additional elements to display when you use the second form?

c) Any particular reason you are doing your drawing in the ListField's paint method and not in the drawListRow,  If you did it in the drawListRow,you wouldn't have to figure out which rows you are allowed to display.

My questions may not be relevant, just tell me if they are not!