postForm() 中的 infiniteProgress 组件问题 - codenameone

infiniteProgress component issue in postForm() - codenameone

我已将所有连接保存在 postForm() 中。我这样做是因为如果我保留 beforeForm() 可能会点击按钮两次或更多次,但问题是这里的 infiniteProgress 组件在表单的所有组件到达之前就被处理掉了。它会导致空白屏幕片刻。如果网络慢,则需要更多时间。怎样才能更方便?

public class GroupConnection {
ArrayList<Map<String,Object>> response;

void groupConnection() {
    ConnectionRequest connectionRequest = new ConnectionRequest() {

        @Override
        protected void readResponse(InputStream input) throws IOException {
            JSONParser jSONParser = new JSONParser();
            Map<String,Object> parsedData = jSONParser.parseJSON(new InputStreamReader(input));
            response = (ArrayList<Map<String, Object>>) parsedData.get("root");
        }

        @Override
        protected void postResponse() {
            super.postResponse(); 
        }
    };
    AllUrl allUrl = new AllUrl();
    connectionRequest.setUrl(allUrl.groupsUrl);
    connectionRequest.setPost(false);
    connectionRequest.setTimeout(8000);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    connectionRequest.setDisposeOnCompletion(d);
    NetworkManager.getInstance().addToQueue(connectionRequest);
 }
}

void postForm(Form f) 方法:

    GroupConnection gc = new GroupConnection();
    gc.groupConnection();
    if (gc.response != null) {
        wrapContainer = new Container();
        for (Map<String, Object> element : gc.response) {
            String tableName = (String) element.get("name");
            String inaugurationDate = (String) element.get("inaugurationdate");
            String areaCode = (String) element.get("areacode");
            String clubAbbr = (String) element.get("clubabbrname");
            String charterDate = (String) element.get("clubcharterdate");

            Container tableNameDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea tableNameData = new TextArea(tableName);
            tableNameDataContainer.add(tableNameData);

            Container inaugurationDateDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea inaugurationDateData = new TextArea(inaugurationDate);
            inaugurationDateDataContainer.add(inaugurationDateData);

            Container areaCodeDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea areaCodeData = new TextArea(areaCode);
            tableDataMeetingTextArea(areaCodeData, izz, areaCodeDataContainer);
            areaCodeDataContainer.add(areaCodeData);

            Container clubAbbrNameDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea clubAbbrNameData = new TextArea(clubAbbr);
            clubAbbrNameDataContainer.add(clubAbbrNameData);

            Container clubCharterDateDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea clubCharterDateData = new TextArea(charterDate);
            clubCharterDateDataContainer.add(clubCharterDateData);

            TableLayout tl1 = new TableLayout(1, 5);
            containerTableDataGroup = new Container(tl1);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(30), tableNameDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(20), inaugurationDateDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(15), areaCodeDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(15), clubAbbrNameDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(20), clubCharterDateDataContainer);

            wrapContainer.add(containerTableDataGroup);
        }
        f.add(wrapContainer);
    }

您需要将代码修改为事件驱动,当响应到达时刷新表单上的数据,尝试对代码进行以下修改:

public class GroupConnection {
ArrayList<Map<String,Object>> response;

void groupConnection(ActionListener callback) {
    ConnectionRequest connectionRequest = new ConnectionRequest() {

        @Override
        protected void readResponse(InputStream input) throws IOException {
            JSONParser jSONParser = new JSONParser();
            Map<String,Object> parsedData = jSONParser.parseJSON(new InputStreamReader(input));
            response = (ArrayList<Map<String, Object>>) parsedData.get("root");
            //the reponse has arrived we need to inform the UI that the data can is ready to be displayed on screen
            Display.getInstance().callSerially(new Runnable(){
             public void run(){
              callback.actionPerformed(null);

             }
            });
        }

        @Override
        protected void postResponse() {
            super.postResponse(); 
        }
    };
    AllUrl allUrl = new AllUrl();
    connectionRequest.setUrl(allUrl.groupsUrl);
    connectionRequest.setPost(false);
    connectionRequest.setTimeout(8000);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog d = ip.showInifiniteBlocking();
    connectionRequest.setDisposeOnCompletion(d);
    NetworkManager.getInstance().addToQueue(connectionRequest);
 }
}

void postForm(final Form f) 方法:

GroupConnection gc = new GroupConnection();
    gc.groupConnection(f, new ActionListener(){
     public void actionPerformed(ActionEvent evt){
        wrapContainer = new Container();
        for (Map<String, Object> element : gc.response) {
            String tableName = (String) element.get("name");
            String inaugurationDate = (String) element.get("inaugurationdate");
            String areaCode = (String) element.get("areacode");
            String clubAbbr = (String) element.get("clubabbrname");
            String charterDate = (String) element.get("clubcharterdate");

            Container tableNameDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea tableNameData = new TextArea(tableName);
            tableNameDataContainer.add(tableNameData);

            Container inaugurationDateDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea inaugurationDateData = new TextArea(inaugurationDate);
            inaugurationDateDataContainer.add(inaugurationDateData);

            Container areaCodeDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea areaCodeData = new TextArea(areaCode);
            tableDataMeetingTextArea(areaCodeData, izz, areaCodeDataContainer);
            areaCodeDataContainer.add(areaCodeData);

            Container clubAbbrNameDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea clubAbbrNameData = new TextArea(clubAbbr);
            clubAbbrNameDataContainer.add(clubAbbrNameData);

            Container clubCharterDateDataContainer = new Container(new FlowLayout(Component.CENTER, Component.CENTER));
            TextArea clubCharterDateData = new TextArea(charterDate);
            clubCharterDateDataContainer.add(clubCharterDateData);

            TableLayout tl1 = new TableLayout(1, 5);
            containerTableDataGroup = new Container(tl1);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(30), tableNameDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(20), inaugurationDateDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(15), areaCodeDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(15), clubAbbrNameDataContainer);
            containerTableDataGroup.add(tl1.createConstraint().widthPercentage(20), clubCharterDateDataContainer);

            wrapContainer.add(containerTableDataGroup);
        }
        f.add(wrapContainer);
        f.revalidate();
     }
    });