java - 黑莓列表字段 : Maintaining row focus during an update

标签 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.

           }

        });

    }

}

最佳答案

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);
       }
    });

在您在评论中发布的代码中,您在执行以下操作之后,使用 getSelectedIndex() 的结果调用 setSelectedIndex()刷新,这永远不会做你想要的。

关于java - 黑莓列表字段 : Maintaining row focus during an update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065908/

相关文章:

java - 手动进化

java - 如何获取SD卡事件?

netbeans - 使用 LWUIT 以 RTL 语言换行文本

java - 黑莓文件连接非法状态异常

java - J2ME的append()方法

java - 创建一个狗类和测试器

java - 将文件复制到HDFS时,如何控制该文件驻留在哪些节点上?

java - 应用程序 Web URL 重定向如何工作?

testing - 在 BlackBerry 浏览器上对网站进行故障排除

黑莓中的json解析?