11-25-2009 04:08 AM
With the help of the forum I have created a customObjectListField class, I am creating an array of these fields so they can be created dynamically. I am calling this class and creating the fields in the following way,
strTemp = new String[1];
CustomObjectListField customObjListField[] = new CustomObjectListField[vct_id.size()];
System.out.println("vector size " + vct_id.size());
for (int i = 0; i < vct_id.size(); i++){
System.out.println(i);
System.out.println(str_desc[i]);
System.out.println("start for");
strTemp[0] = str_desc[i];
customObjListField[i] = new CustomObjectListField(str_id[i], str_desc[i], str_plcyNo[i], str_value[i]);
customObjListField[i].set(strTemp);
customObjListField[i].setRowHeight(60);
add(customObjListField[i]);
System.out.println("control added :" + i);
System.out.println("next for");
}
drawGraph();
System.out.println(" finished ");
}
I also have a system.out in the class that outputs "in class", so when I run the code I get the following output.
vector size 3
0
description1
start for
control added :0
next for
1
description2
start for
control added :1
next for
2
description3
start for
control added :2
next for
finished
in class
0
description3
in class
0
description3
in class
0
description3
in class
What seems to be happening is that it loops through the for loop, then when finished the class is called where it will create the three list fields, but all for description3. I have no idea why this behavour is happening, any help with this would be much appreciated!
11-25-2009 04:29 AM
I think this is because you are passing the same strTemp array to all three lists, while overwriting the contents of the array in every iteration of the loop. As a result, all three list fields reference the same one-element array with "description3" as the only element.
11-25-2009 04:38 AM
The output matches the for loop, which is the only code you have supplied us. I think we need more if we are to investigate this. It would seem that the System.out lines that prints this
0
description3
in class
are not included in the snippet, making it, as far as I can tell, impossible to figure out what is going on.
11-25-2009 04:56 AM
Thanks for the reply. Are you refering to,
customObjListField[i].set(strTemp);
I think this is down to a lack of understanding on my behalf of what this does. I thought the class would be called with each loop in the for loop, not just at the end. Not sure what to do with this now as I have just replaced it with str_desc and it created 9 rows instead of the three, all 9 rows were description3.
11-25-2009 05:00 AM - edited 11-25-2009 05:01 AM
ObjectListField.set sets items to be displayed in the list. You are passing the same array to all three lists. That's why all three lists show the same items (I presume that's what your post is about). You can fix your code by moving the strTemp array into the loop. This will create three different arrays, one for each iteration of the loop, and will thus initialize the three lists with three different one-item arrays.
11-25-2009 05:01 AM
Hi Peter, Here is the code from the class,
public class CustomObjectListField extends ObjectListField
{
private static String[] _nameStr, _detailsStr, _colorStr, _valueStr, _id, _plcyNoStr;
private static String _StrName, _StrDetails, _StrColor, _StrValue, _StrId, _StrPlcyNo;
private static String _pageType;
private int _index;
private Bitmap icon = Bitmap.getBitmapResource("GraphIconClear.png");
private Bitmap arrow = Bitmap.getBitmapResource("arrow.PNG");
private ConnectionThread _connectionThread = new ConnectionThread();
public CustomObjectListField(String id, String detailsStr, String plcyNoStr, String valueStr)
{
_StrId = id;
_StrDetails = detailsStr;
_StrPlcyNo = plcyNoStr;
_StrValue = valueStr;
_pageType = "test";
}
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
_index = index;
FontFamily font;
Font intFont = null;
int offsetY = (this.getRowHeight() - icon.getHeight()) / 2;
int screenWidth = Display.getWidth();
System.out.println("in class");
loginScreen.fieldFocus = index;
System.out.println(loginScreen.fieldFocus);
graphics.setColor(9219003);
graphics.clear();
//draw the coloured square
graphics.setColor(9219003);
graphics.fillRect(5,y+5,40,40);
//draw the seperator line
graphics.setColor(8553090);
graphics.fillRect(0,y+58,screenWidth,1);
graphics.drawBitmap(5, y+5, icon.getWidth(), icon.getHeight(), icon, 0, 0);
graphics.drawBitmap(screenWidth - 40, y + offsetY + 10, icon.getWidth(), icon.getHeight(), arrow, 0, 0);
graphics.setColor(0);
// display Line 1 of Text
intFont = font.getFont(Font.BOLD,16);
graphics.setFont(intFont);
graphics.drawText(_StrDetails,icon.getWidth() + 13, y+5,(DrawStyle.LEFT + DrawStyle.ELLIPSIS + DrawStyle.TOP), width);
// this is the description
System.out.println(_StrDetails);
}
}
Hope this helps. The bit that I do not understand is why this is only called after the for loop finishes?
11-25-2009 05:02 AM
Thanks Klyubin, I think I do what you saying already in the for loop,
strTemp[0] = str_desc[i];
11-25-2009 05:04 AM
System.out.println(_StrDetails) is only called after the loop finishes, most likely because the loop is invoked on the event thread. The painting of the list(s), which invokes the above println, is posted to the event queue, and is thus also invoked on the event thread, but only after your loop completes and frees up the event thread for the processing of the next UI event (e.g., paint the list field(s)).
11-25-2009 05:25 AM
Thank you, I assume I can not do what I want to do in this way?
11-25-2009 06:33 AM
If you already have a realized container and start adding stuff, then yeah you are probably dealing
with multiple threads. There is no reason for the framework to believe you want each little widget
you add to force the whole thing to get updated so it would actuall make sense in many cases to
add a lot of thing and only then start repainting the screen after the group add is done. Off hand, if you are holding eventlock
through the loop that probably achieves this end since it is unlikely add() goes around updating
the screen and no one else can. Also, I think there is something like updateDisplay to force a repaint etc.
If you step through it in debugger or print out the currentThread.toString() you can probably see who is doing what,
and it may help too if your println is synchronized LOL. I see some issues with thread switching where I can
get entries in log thare may be out of order by a few ms.
GUI's never make sense LOL.