Blackberry ListField:在更新期间维护行焦点

时间:2012-12-28 07:02:30

标签: java blackberry java-me

使用下面的代码刷新包含xml Web服务中返回的数据的ListField。但是在刷新或刷新之后,它将焦点设置为ListField中的第一行。我不希望这样。我希望它在刷新后保持当前的焦点,以便用户甚至不知道有刷新。

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 个答案:

答案 0 :(得分:1)

作为I suggested in the comments after my answer yesterday,您需要在刷新列表之前记录当前关注的行,然后在更新后立即再次设置焦点行。

因此,例如,在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);
       }
    });

在您在评论中发布的代码中,您在{/ 1}} 之后调用了setSelectedIndex()的结果进行了更新,这将永远不会达到您想要的效果。