2012-04-23 75 views
0

我需要通過循環兩組數據來創建自定義列表。下面是我正在使用的,但是當我將它附加到我的列表視圖時,我只獲取最後一條記錄。我試着移動this.CoeListitem =新列表,我知道是第一個循環上方的問題,但沒有返回任何記錄。那麼,如何設置這個來創建具有正確記錄數量的List。 這裏是我的如何創建一個自定義列表,包含循環中的幾列

public class CoeList 

[PrimaryKey, AutoIncrement] 
public long Id { get; set; } 
public string Name { get; set; } 
public string CreateDt { get; set; } 

這裏是我的循環,首先是讓我科項目,二是讓所有去各個科項目成年人可能是許多這就是爲什麼我需要兩個循環。

 //new loop 
    List<Coe> allCoe = (List<Coe>)((OmsisMobileApplication)Application).OmsisRepository.GetAllCoe(); 
    if (allCoe.Count > 0) 
    { 
     foreach (var Coeitem in allCoe) 
     { 
     //take the coe id and get the adults 
     List<Adult> AdultsList = (List<Adult>)((OmsisMobileApplication)Application).OmsisRepository.GetAdultByCoeMID(Coeitem.Id); 
     if (AdultsList.Count > 0) 
     { 
      foreach (var AdltItem in AdultsList) 
      { 
      CoeNames += "; " + AdltItem.LName + ", " + AdltItem.FName; 
      } 
     } 
      CoeNames = CoeNames.Substring(1); 
      //ceate new list for coelist 
      this.CoeListitem = new List<CoeList>() 
      { 
       new CoeList() { Id = Coeitem.Id, CreateDt = Coeitem.CreateDt, Name = CoeNames } 
      }; 
     } 
    } 
    // End loop 
    _list.Adapter = new CoeListAdapter(this, CoeListitem); 

回答

0

你的問題在於這樣一個事實,隨着循環的每次迭代你重新創建整個列表,並失去所有之前的項目(您分配一個列表中,只有一個項目,給變量)。因此,在循環結束時,您只有一個項目。

您必須創建一個列表以外的循環,並且只將每個項目添加到lop主體中的列表中。

// create the new list first 
this.CoeListitem = new List<CoeList>(); 

var application = (OmsisMobileApplication) Application; 
List<Coe> allCoe = (List<Coe>) application.OmsisRepository.GetAllCoe(); 
foreach (var Coeitem in allCoe) //new loop 
{ 
    //take the coe id and get the adults 
    List<Adult> AdultsList = (List<Adult>) application.OmsisRepository.GetAdultByCoeMID(Coeitem.Id); 
    foreach (var AdltItem in AdultsList) 
    { 
     CoeNames += "; " + AdltItem.LName + ", " + AdltItem.FName; 
    } 
    CoeNames = CoeNames.Substring(1); 

    // Add the item to the existing list 
    this.CoeListitem.Add(new CoeList { Id = Coeitem.Id, CreateDt = Coeitem.CreateDt, Name = CoeNames }); 
} // End loop 

// give the list to the adapter 
_list.Adapter = new CoeListAdapter(this, CoeListitem); 

希望這會有所幫助。

相關問題