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 に答える