Yasir Rajab
Yasir Rajab

Reputation: 1

Blackberry Thread Image from JSON

I am looking for a way to display images on my ListField from a background thread. First in my drawListRow i try this

path = (String) imagePaths.elementAt(index);
bit = connectServerForImage(path);

g.drawBitmap(xText, y + yText, 80, 200, bit, 0, 0);

but can't scroll smoothly throughout the list, and they say do not do networking or other blocking operations on the UI. But i also try this

private class imgConnection extends Thread
{      
        public imgConnection() {
                super();
        }

        public void run() {            

                try {
                    for (int i = 0; i < imagePaths.size(); i++)
                    {
                        final int index = i;                              
                        String path = imagePaths.elementAt(index).toString();
                        bit = connectServerForImage(path);
                        image.addElement(bit);

                    }
                }
                catch (Exception e)
                {
                    System.out.println(e.toString());                        
                }

                UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {        
                        _list.setSize(image.size());
                        subManager.add(_list);                        
                        screen.invalidate();
                        }  
                        });


        }
}

public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
    bit = (Bitmap) image.elementAt(index);
    g.drawBitmap(xText, y + yText, 80, 200, bit, 0, 0);
}

but nothing happens. Any idea, comments.


You are right, i just started java development 2 weeks ago particularly BB development and i try this link. I want to add a background thread to download image after i got the path url from json return.

first thread:

_connectionthread = new Connection();
_connectionthread.start();

private class Connection extends Thread
{
    public Connection()
    {
       super();
    }

    public void run() {      
        try {}
        catch (Exception e) {}
    } 
}

second thread:

_imgConnectionThread = new ImgConnection();
_imgConnectionThread.start();

private class ImgConnection extends Thread
{      
        public ImgConnection() {
           super();
        }

        public void run() {            
                try {
                }
                catch (Exception e)
                {
                }

        }
}

how to update images on ListField?

Upvotes: 0

Views: 263

Answers (2)

Eugen Martynov
Eugen Martynov

Reputation: 20130

Answer is based on code from - pastebin.com/90UKTHzP

Terrible code! It's really hard to read and undersand! It looks like you copy pasted several examples from different locations. Also you overriding default behavior with same behavior. Also MainScreen already has VerticalManagerField. Also you're adding list every iteration to manager which will cause IAE. And main one thread is depended on result of second one. They start at the same time, but getting json from server and it's processing could take longer time, so image thread most probably will finish his run without any result.

So main recommendation to fix it - read clean code book! Read more about java development - conventions, multithreading. Read about BB development - UI api, networking.

And finally - start only one thread to get and parse json. After you get it finished - start another thread to get images.

There some minor things that could save you more battery and processor time also - start loading images on demand - when it painted or going to be painted (user scrolls list).

Upvotes: 2

Michael Donohue
Michael Donohue

Reputation: 11876

By convention, Java class names start with a capital letter, so imgConnection should really be ImgConnection.

In your sample code, I don't see imgConnection being instantiated anywhere, and I don't see any call to Thread.start(), which is the way a thread i started. Without Thread.start() it is not surprising nothing is happening - the thread is never starting.

Upvotes: 1

Related Questions