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