2013-05-30 72 views
-1

public Details getDetails(String id)throws InvokerException {詳細信息details = new Details();如何在迭代列表時返回所有值?

try { 

    URL url = new URL(BaseUrl, "/cxf/query/ask?id=" + id); 
    LOGGER.trace("URL: {}", url); 

    String xml = queryStore(url); 
    LOGGER.trace("Query result: {}", xml); 

    details = new Details(); 
    InputSrc source = new InputSrc(new StringReader(xml)); 
     ResultsContentHandler handler = new ResultsContentHandler(); 

    XMLReader reader = XMLReaderFactory.createXMLReader(); 
    reader.setContentHandler(handler); 
    reader.parse(source); 

    for (Hashtable<String,String> result : handler.getResultSet()) { 
     String baseId = result.get("baseId"); 
     ArrayList<Details> list = getHistoryDetails(baseId); 

     for(Details t : list) { 
     details.setStatus(t.getStatus()); 
     } 
    } 
} catch (Exception e) { 
    throw new InvokerException(e); 
} 
return details; 

}

我想要實現的是返回細節ArrayList中的所有項目。例如,我期望不止一種身份,但目前我只能獲得一種身份。

詳細類

public class Details { 
    private Core core; 
    private String department; 
    private GregorianCalendar timestampReceived; 
    private GregorianCalendar timestampReported; 
    private String status; 
    private GregorianCalendar timestampStatus; 
    private String explanation; 

    public Details(){} 

    public Details(Core core, 
      String department, GregorianCalendar timestampReceived, 
      GregorianCalendar timestampReported, String status, 
      GregorianCalendar timestampStatus, String explanation) { 
     super(); 
     this.core = core; 
     this.department = department; 
     this.timestampReceived = timestampReceived; 
     this.timestampReported = timestampReported; 
     this.status = status; 
     this.timestampStatus = timestampStatus; 
     this.explanation = explanation; 
    } 
    public Core getCore() { 
     return core; 
    } 

    public String getDepartment() { 
     return department; 
    } 
    public GregorianCalendar getTimestampReceived() { 
     return timestampReceived; 
    } 

    public GregorianCalendar getTimestampReported() { 
     return timestampReported; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public GregorianCalendar getTimestampStatus() { 
     return timestampStatus; 
    } 

    public String getExplanation() { 
     return explanation; 
    } 

    public void setCore(Core core) { 
     this.core = core; 
    } 

    public void setDepartment(String department) { 
     this.department = department; 
    } 

    public void setTimestampReceived(GregorianCalendar timestampReceived) { 
     this.timestampReceived = timestampReceived; 
    } 

    public void setTimestampReported(GregorianCalendar timestampReported) { 
     this.timestampReported = timestampReported; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public void setTimestampStatus(GregorianCalendar timestampStatus) { 
     this.timestampStatus = timestampStatus; 
    } 

    public void setExplanation(String explanation) { 
     this.explanation = explanation; 
    } 


} 
+0

然後你不是以你自己的方式填充你的ArrayList。改變你的期望或調查他們。我們不會爲你做。 –

+0

很多人可能會說*返回'List '而不是*,*移動'Detail detail = new Details();'在循環*內,並*用這個新實例填充你的列表*但是看起來你沒有'你自己試過任何東西...... –

+0

這是什麼'Details'類?它是如何工作的?向我們展示一些代碼! –

回答

0

您需要返回List<Details>,不只是細節。所以你想在你的代碼示例中返回變量「list」。

+0

如果你分析整個代碼,你會看到很多錯誤,你應該重新編輯這個答案併爲它們提供一個解決方案。 –

+2

這可能會讓他朝着正確的方向前進。 – Michael

+0

甚至沒有... –

1

你不能只在一個調用的方法返回多個值,但你應該做的是返回一個ArrayList:

public List<Details> getDetails(String id) throws InvokerException { 
    List<Details> details = new ArrayList<Details>(); 

    try { 

     URL url = new URL(BaseUrl, "/cxf/query/ask?id=" + id); 
     LOGGER.trace("URL: {}", url); 

     String xml = queryStore(url); 
     LOGGER.trace("Query result: {}", xml); 

     InputSrc source = new InputSrc(new StringReader(xml)); 
      ResultsContentHandler handler = new ResultsContentHandler(); 

     XMLReader reader = XMLReaderFactory.createXMLReader(); 
     reader.setContentHandler(handler); 
     reader.parse(source); 

     Details detail; 
     for (Hashtable<String,String> result : handler.getResultSet()) { 
      String baseId = result.get("baseId"); 
      ArrayList<Details> list = getHistoryDetails(baseId); 

      for(Details t : list) { 
      detail = new Details(); 
      detail.setStatus(t.getStatus()); 
      details.add(detail); 
      } 
     } 
    } catch (Exception e) { 
     throw new InvokerException(e); 
    } 
    return details; 
} 
+0

我不明白爲什麼要創建一個新的'Detail'實例來設置'status'屬性,然後將它添加到列表中。爲什麼不把'ArrayList

list'中的'detail'對象添加到'details'中? –

+1

我同意你的觀點,但是因爲不是我的代碼,所以我試着糾正它與他給我們的那個差不多:),也許他只想要狀態而不是所有對象可以攜帶的信息。 – KBorja

+0

狀態只是Details類的其中一個項目。 – Syk

0

創建ArrayList<Details>()和細節添加到它。之後,返回這個列表。

代碼:

List<Details> detailsToReturn = new ArrayList<>(); 

for (Hashtable<String,String> result : handler.getResultSet()) { 
      String baseId = result.get("baseId"); 
      ArrayList<Details> list = getHistoryDetails(baseId); 

      for(Details t : list) { 
       Details temp = new Details(); 
       temp.setStatus(t.getStatus(); 
       detailsToReturn.add(temp); 
      } 
     } 

return detailsToReturn; 

您所提供的代碼是很清楚,所以如果這不是你想要的,我建議你把它清除掉我們。

0

您只能創建一個Details對象(實際上是兩個,但是第一個被扔掉了)。如果要保留從ArrayList檢索的每個Details對象,則每次迭代for循環時都需要創建一個新的Details對象。您還應該將這些添加到新的List

當然,您只需將getHistoryDetails()獲得的每個ArrayList添加到新列表中,即可節省大量工作量。

+0

Code-Guru可以舉一個例子嗎? – Syk