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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
mabs
Posts: 129
Registered: ‎09-21-2009

code not running in order?

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!

 

 

 

Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009

Re: code not running in order?

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.

Please use plain text.
Developer
peter_strange
Posts: 17,958
Registered: ‎07-14-2008

Re: code not running in order?

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.

Please use plain text.
Developer
mabs
Posts: 129
Registered: ‎09-21-2009

Re: code not running in order?

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.

 

 

 

 

Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009

Re: code not running in order?

[ Edited ]

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.

Please use plain text.
Developer
mabs
Posts: 129
Registered: ‎09-21-2009

Re: code not running in order?

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?

 

 

 

Please use plain text.
Developer
mabs
Posts: 129
Registered: ‎09-21-2009

Re: code not running in order?

Thanks Klyubin,  I think I do what you saying already in the for loop,

 

strTemp[0] = str_desc[i];

 

 

Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009

Re: code not running in order?

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

Please use plain text.
Developer
mabs
Posts: 129
Registered: ‎09-21-2009

Re: code not running in order?

Thank you, I assume I can not do what I want to do in this way?

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008

Re: code not running in order?

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.

 

 

 

Please use plain text.