2012-05-05 86 views
2

我想添加各種記錄,以通過單個鍵值添加/檢索。如何將不同類型的對象添加到Android中的散列表中?

每個數據記錄應該有以下參數;

public class WebRecord { 
     Integer UniqueCustomerID; 
     Integer Count; // Number of websites for a given customer 

//Repeat the following 'Count' number of times 
     { // Each Record for a given Website for the same customer 
    String BusinessName, Websites, siteTag; 
    Integer uniqueWebID, BusinessTypeID; 
     } 

} 

我希望這樣的記錄基於從Web服務獲得的計數值存在。 我打算使用Hashmap如下:

有人可以告訴我如何在HASHMAP中實現該數據結構嗎? 我想使用一個鍵值UniqueCustomerID來獲取這些記錄;

+2

這實際上與android沒有任何關係。你應該刪除android標籤。 – Louth

回答

1

不好用java概念,但我認爲你可以做,

 class WebRecord{ 
      int UniqueCustomerID; 
      int Count; 
    } 

    class ContactRecord{ 
     String BusinessName, Websites, siteTag; 
     int uniqueWebID, BusinessTypeID; 
    } 

而且在你的java文件

MyActivity{ 
      public Map<WebRecord,ArrayList<ContactRecord>> map=new HashMap<WebRecord,ArrayList<ContactRecord>>(); 

     //now create one object of web record 

     //create arraylist of ContactRecord depending upon "count" variable's value 

     // fill the above both and put into map 

      map.put(webRec, arrContact); 
    } 
0

如果你有一些ContactRecord對象等於Count值,那麼你不需要明確存儲該值。

鑑於上面的數據結構,你可以這樣做。

public HashMap<Integer, ArrayList<ContactRecord>> map; 

哪裏的關鍵地圖是UniqueCustomerID。要發現Count值,您可以像這樣詢問地圖中的列表。

map.get(customerId).size(); 
相關問題