Blackberry ListField: Maintaining row focus during an update

雨燕双飞 提交于 2019-12-13 06:02:00

问题


After using the code below to refresh a ListField containing data returned in an xml web service. But after refresh or on refresh it sets focus to the first row in the ListField. I don't want that. I want it to maintain its current focus after refresh so that a user wont even know that there was a refresh.

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() {

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

           }

        });

    }

}

回答1:


As I suggested in the comments after my answer yesterday, you need to record the currently focused row before you refresh your list, and then set the focused row again, immediately after the update.

So, for example, in the WebServiceTask:

    UiApplication.getUiApplication().invokeLater(new Runnable() {
       public void run() {
          int currentIndex = listUsers.getSelectedIndex();
          int scrollPosition = getMainManager().getVerticalScroll();

          //Return vector sze from the handler class
          listUsers.setSize(handler.getItem().size());

          listUsers.setSelectedIndex(currentIndex);
          getMainManager().setVerticalScroll(scrollPosition);
       }
    });

In the code you posted in your comment, you were calling setSelectedIndex() with the result of getSelectedIndex() after you did the refresh, which will never do what you want.



来源:https://stackoverflow.com/questions/14065908/blackberry-listfield-maintaining-row-focus-during-an-update

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