2015-11-05 32 views
0

我從PHP獲取結果,並將它們解析爲字符串數組 ParseResults [0]是從數據庫返回的ID。僅針對數組列表中的每個ID執行一次

我想要做的是製作一個消息框,它只顯示一次(直到應用程序重新啓動當然)。

我的代碼看起來像這樣,但我無法弄清楚是什麼阻止它正常工作。

public void ShowNotification() { 
    try { 

     ArrayList<String> SearchGNArray = OverblikIntetSvar(Main.BrugerID); 
     // SearchGNArray = Gets undecoded rows of information from DB 
     for(int i=0; i<SearchGNArray.size(); i++){ 

      String[] ParseTilArray = ParseResultater(SearchGNArray.get(i)); 
      // ParseToArray = Parse results and decode to useable results 
      // ParseToArray[0] = the index containing the ID we'd like 
      // to keep track of, if it already had shown a popup about it 

      if (SearchPopUpArray.size() == 0) { 
       // No ID's yet in SearchPopUpArray 
       // SearchPopUpArray = Where we'd like to store our already shown ID's 
       SearchPopUpArray.add(ParseTilArray[0]); 

       // Create Messagebox 

      } 

      boolean match = false ; 
      for(int ii=0; ii<SearchPopUpArray.size(); ii++){ 

       try { 
        match = SearchPopUpArray.get(ii).equals(ParseTilArray[0]); 

       } catch (Exception e) { 
        e.printStackTrace(); 

       } 

       if(match){ 
        // There is a match 
        break; // Break to not create a popup 

       } else { 

        // No match in MatchPopUpArray 
        SearchPopUpArray.add(ParseTilArray[0]); 

        // Create a Messagebox 

       } 
      } 



     } 

      } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

截至目前我有2行,所以應該有兩個ID。有101和102這表明102一次,然後它只是不斷約101

回答

0

你是不是增加正確的變量在第二個for循環創建提示消息框:

for(int ii = 0; ii <SearchPopUpArray.size();i++){ 
             /* ^
              | 
              should be ii++ 
             */ 
} 

這可能是幫助使用更具描述性的變量名狀indexGNindexPopup來避免這類問題的

+0

它現在增加了ii ++,但我得到一個死代碼警告......所以它只是通過循環運行一次,我猜? –

+0

根據警告,哪部分代碼死了? – nicopico

+0

for循環包含ii ++ 如果我嘗試System.out.println(ii)匹配後= ... 我得到值(0)回來 - 所以它永遠不會增加 –

0

我已經刪除了第二個for循環:

  for(int ii=0; ii<SearchPopUpArray.size(); ii++){ 

      try { 
       match = SearchPopUpArray.get(ii).equals(ParseTilArray[0]); 

      } catch (Exception e) { 
       e.printStackTrace(); 

      } 

      if(match){ 
       // There is a match 

      } else { 

       // No match in MatchPopUpArray 
       SearchPopUpArray.add(ParseTilArray[0]); 

       // Create a Messagebox 

      } 
     } 

並換成

  if (SearchPopUpArray.contains(ParseTilArray[0])) { 
       // Match 
      } else { 
       // No match i MatchPopUpArray 
       SearchPopUpArray.add(ParseTilArray[0]); 

       // Create a Messagebox 
      } 

更簡單。

相關問題