Refresh Screen to Update ListField with new Data

为君一笑 提交于 2019-12-25 06:39:10

问题


I am using a ListField Control to display data returned from xml webservice. I want to refresh the ListField or the screen every minute to update the ListField with new records or data.

I tried using the code below but it is not working properly (It is hanging).

public MyApp() {
    // Push a screen onto the UI stack for rendering.
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            UiApplication.getUiApplication().pushScreen(new MyScreen());
        }
    },5000,true);
}
ResponseHandler handler = new ResponseHandler();
ListField listUsers = new ListField(handler.getItem().size());

public MyScreen() {
    setTitle("yQAforum");
    //Fetch the xml from the web service
    String wsReturnString = GlobalV.Fetch_Webservice("myDs");
    //Parse returned xml
    SAXParserImpl saxparser = new SAXParserImpl();
    ByteArrayInputStream stream = new ByteArrayInputStream(wsReturnString.getBytes());
    try {
        saxparser.parse( stream, handler );
    } 
    catch ( Exception e ) {
        response.setText( "Unable to parse response.");
    }
    //Return vector sze from the handler class
    listUsers.setSize(handler.getItem().size());
    listUsers.setCallback(this);
    listUsers.setEmptyString("No Users found", 0);
    add(listUsers);
}

回答1:


You are attempting to fetch data from your webservice on the UI thread. That's almost always the wrong thing to do.

The UI thread (also known as the main thread) is responsible for drawing the UI, and tracking user actions, like touches, or navigation via a trackpad/trackball. If the UI thread is blocked waiting for a remote web server to respond, it cannot service the UI.

There's a couple changes you should make:

public MyApp() {
    // Push a screen onto the UI stack for rendering.
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            UiApplication.getUiApplication().pushScreen(new MyScreen());
        }
    },5000,true);
}

should be changed to

public MyApp() {
    // Push a screen onto the UI stack for rendering.
    pushScreen(new MyScreen());
}

The MyApp() constructor will already be called on the UI thread, so there is no need to use invokeLater() to perform the pushScreen() call on the UI thread. It already will be called on the UI thread, if run from within the MyApp constructor. Also, the 5000 msec delay isn't really helpful. This will just delay the startup of your app by 5 seconds, which users will hate.

If you are trying to implement a splash screen, or something similar, when the app starts up, please search stack overflow for "BlackBerry splash screen", and I'm sure you'll find results.

Now, once your MyScreen class is created, you should take care not to fetch web service results from the UI thread. The MyScreen constructor will be run on the UI thread. If you want, you can initiate a web service request on a background thread, once the screen is shown. One way to do that is to use onUiEngineAttached():

protected void onUiEngineAttached(boolean attached) {
    if (attached) {
        // TODO: you might want to show some sort of animated
        //  progress UI here, so the user knows you are fetching data

        Timer timer = new Timer();
        // schedule the web service task to run every minute
        timer.schedule(new WebServiceTask(), 0, 60*1000);
    }
}

public MyScreen() {
    setTitle("yQAforum");

    listUsers.setEmptyString("No Users found", 0);
    listUsers.setCallback(this);
    add(listUsers);
}

private class WebServiceTask extends TimerTask {
    public void run() {
        //Fetch the xml from the web service
        String wsReturnString = GlobalV.Fetch_Webservice("myDs");
        //Parse returned xml
        SAXParserImpl saxparser = new SAXParserImpl();
        ByteArrayInputStream stream = new ByteArrayInputStream(wsReturnString.getBytes());
        try {
           saxparser.parse( stream, handler );
        } 
        catch ( Exception e ) {
           response.setText( "Unable to parse response.");
        }
        // now, update the UI back on the UI thread:
        UiApplication.getUiApplication().invokeLater(new Runnable() {
           public void run() {
              // TODO: record the currently selected, or focused, row

              //Return vector sze from the handler class
              listUsers.setSize(handler.getItem().size());
              // Note: if you don't see the list content update, you might need to call
              //   listUsers.invalidate();
              // here to force a refresh.  I can't remember if calling setSize() is enough.

              // TODO: set the previously selected, or focused, row
           }
        });
    }
}

You'll need to add some error handling, in case the web service doesn't respond, or takes longer than a minute (you wouldn't want to be making a new request, if the last one hadn't finished).

But, this should get you started.

Note: once you fix the problem with running network code on the UI thread, you may still find that your code doesn't work. There could be problems fetching the web service data. You'll have to debug that. I am only showing you one problem with the code posted. If you still have problems with the web service fetch, post another question (with the UI thread problem fixed). Thanks.



来源:https://stackoverflow.com/questions/14055392/refresh-screen-to-update-listfield-with-new-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!