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

时间:2016-05-31 11:18:03

标签: 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);
    }

1 个答案:

答案 0 :(得分:1)

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

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