0

在使用下面的代码刷新包含在 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.

           }

        });

    }

}
4

1 回答 1

1

正如我在昨天回答后的评论中所建议的那样,您需要在刷新列表之前记录当前关注的行,然后在更新后立即再次设置关注的行。

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

在您在评论中发布的代码中,您调用的是刷新setSelectedIndex()的结果,这永远不会做您想要的。getSelectedIndex()

于 2012-12-29T03:26:18.943 回答