开发者

How to refresh Fields during activity inside a loop?

I'm trying 开发者_JAVA技巧to download images and display them in a 5x5 grid, and I want each image to show up as soon as it's downloaded.

I have a VerticalFieldManager, and to it I add 5 HorizontalFieldManagers. As I download each image (using HttpConnection), I convert it to a Bitmap then place it in a BitmapField then add the field to one of the HorizontalFieldManagers.

This works, except that I don't see any of the images until after all 25 have been downloaded, at which point I see them all.

I've tried calling invalidate() on everything involved after each image is downloaded, but it doesn't seem to have any effect.


Are you doing the download off the UI event thread? If not, the download will block all UI updates until finished, which would explain the behavior you see.

edit based on new information in comment:

HttpConnection is blocking, so you need to do the IO operation on a thread outside of the UI thread. Your application is downloading the images fast enough that blocking the UI thread isn't killing the whole app. On a slow connection, your app would be killed by the OS while doing these downloads.

This interface performs blocking Input and Output operations. An application will lock if an implementation of this interface opens a connection from within the main event thread. Prevent an application from locking by opening a connection from within a thread that is separate from the main event thread. SeeConnector for more information.


1) You should download and create BitmapField on a separate (non-UI) Thread.

2) When you have a BitmapField to add, then do something like this:

final BitmapField b = ... // your code to get the BitmapField
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
    public void run() {
        yourContainer.add(b);
        yourContainer.invalidate(); // may not need this - try comment out
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜