2016-05-31 19 views
1

我已將所有連接保留在postForm()中。我這樣做是因爲如果我保留beforeForm(),可以點擊兩次或更多按鈕,但問題是在這裏,infiniteProgress組件會在表單的所有組件到達之前處理。它導致了片刻的白色屏幕。如果網絡速度較慢,則需要更多時間。我怎樣才能讓它更方便?postForm()中的infiniteProgress組件問題 - codenameone

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

無效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

您需要修改你的代碼是事件驅動的,當響應到達刷新表單上的數據,請嘗試以下修改您的代碼:

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

無效postForm(最終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(); 
    } 
    }); 
+0

如果我使用addToQueue而不是addToQueueAndWait,則不會顯示任何數據。我在其他連接中也嘗試了它,但是它給出了對話框,用於檢查您的網絡或空白表單。 –

+0

我假設你以某種方式顯示響應中的數據,雖然從上面的代碼中不清楚如何。您需要在返回響應後將項目添加到屏幕,然後重新驗證表格 – Chen

+0

我已替換上述所有代碼。我想我已經完成了你所說的所有事情,但數據並未顯示出來。 –