2016-11-08 22 views
2

當我嘗試檢索listConCard單個對象(在我的活動課ContactCard.class),「CONNAME」所有contactCards正確接收,但我沒有得到來自其兄弟陣列「conPhoneType」,「conPhoneValue」正確的數據, 「conEmailType」,「conEmailValue」。模型類對象列表中的數據沒有正確檢索?

listConCard是我的模型類ContactCardModel.class的對象列表。

我加入在listConCard 「ContactCardModel」 目的如下

listConCard.add(new ContactCardModel(conName, conPhoneType, conPhoneValue, conEmailType, conEmailValue)); 

和檢索 「ContactCardModel」 對象的數據如下兩個陣列getConPhoneType和getConPhoneValue的

for(int i = 0; i < listConCard.size(); i++){ 

    Log.e("InAdp", listConCard.get(i).getConName()); // name is returing correctly. 

    for(int x = 0; x < listConCard.get(i).getConPhoneType().size(); x++) 
     Log.e("InAdp conPhone", listConCard.get(i).getConPhoneType().get(x) + ": " + listConCard.get(i).getConPhoneValue().get(x)); 


    for(int x = 0; x < listConCard.get(i).getConEmailType().size(); x++) 
     Log.e("InAdp conEmail", listConCard.get(i).getConEmailType().get(x) + ": " + listConCard.get(i).getConEmailValue().get(x)); 

} 

值返回0的元素兩個陣列的getConEmailType

值和getConEmailValueare返回1個元件

大小conPhoneType和conPhoneValue的是conEmailType和conEmailValue的相同

大小是相同

ContactCard.class

String conName; 
List<String> conPhoneType; 
List<String> conPhoneValue; 
List<String> conEmailType; 
List<String> conEmailValue; 
List<ContactCardModel> listConCard; 

int i = 1; 

    for (AddressBookContact addressBookContact : list) { 

     conName = addressBookContact.name; 

     conPhoneType.clear(); 
     conPhoneValue.clear(); 
     if(addressBookContact.phones != null) { 

      for (int x = 0; x < addressBookContact.phones.size(); x++) { 
       int type = (int) addressBookContact.phones.keyAt(x); 
       String typeName = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(addressBookContact.res, type, ""); 
       String phValue = addressBookContact.phones.valueAt(x); 

       conPhoneType.add(typeName); 
       conPhoneValue.add(phValue); 
      } 
     } 

     conEmailType.clear(); 
     conEmailValue.clear(); 
     if(addressBookContact.emails != null){ 

      for(int x = 0; x < addressBookContact.emails.size(); x++){ 
       int type = (int) addressBookContact.emails.keyAt(x); 
       String typeName = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(addressBookContact.res, type, ""); 
       String emailValue = addressBookContact.emails.valueAt(x); 

       conEmailType.add(typeName); 
       conEmailValue.add(emailValue); 
      } 
     } 

     listConCard.add(new ContactCardModel(conName, conPhoneType, conPhoneValue, conEmailType, conEmailValue)); 

    } 

ContactCardModel.class

public class ContactCardModel { 

String conName; 
List<String> conPhoneType; 
List<String> conPhoneValue; 
List<String> conEmailType; 
List<String> conEmailValue; 

public ContactCardModel(String mConName, List<String> mConPhoneType, List<String> mConPhoneValue, 
         List<String> mConEmailType, List<String> mConEmailValue){ 

    conPhoneType = new ArrayList<String>(); 
    conPhoneValue = new ArrayList<String>(); 
    conEmailType = new ArrayList<String>(); 
    conEmailValue = new ArrayList<String>(); 

    this.conName = mConName; 
    this.conPhoneType = mConPhoneType; 
    this.conPhoneValue = mConPhoneValue; 
    this.conEmailType = mConEmailType; 
    this.conEmailValue = mConEmailValue; 
} 

public String getConName() { 
    /*Log.e("InAdp conName", this.conName);*/ 
    return conName; 
} 

public void setConName(String conName) { 
    this.conName = conName; 
} 

public List<String> getConPhoneType() { 
    /*for(int i = 0; i < this.conPhoneType.size(); i++){ 
     Log.e("InAdp conPhone", this.conPhoneType.get(i)*//* + ": " + this.conPhoneValue.get(i)*//*); 
    }*/ 
    return conPhoneType; 
} 

public void setConPhoneType(List<String> conPhoneType) { 
    this.conPhoneType = conPhoneType; 
} 

public List<String> getConPhoneValue() { 
    return conPhoneValue; 
} 

public void setConPhoneValue(List<String> conPhoneValue) { 
    this.conPhoneValue = conPhoneValue; 
} 

public List<String> getConEmailType() { 
    /*for(int i = 0; i < this.conEmailType.size(); i++){ 
     Log.e("InAdp conEmail", this.conEmailType.get(i)*//* + ": " + this.conEmailValue.get(i)*//*); 
    }*/ 
    return conEmailType; 
} 

public void setConEmailType(List<String> conEmailType) { 
    this.conEmailType = conEmailType; 
} 

public List<String> getConEmailValue() { 
    return conEmailValue; 
} 

public void setConEmailValue(List<String> conEmailValue) { 
    this.conEmailValue = conEmailValue; 
} 

} 

回答

2

嘗試以下修改。無法運行和測試代碼,因爲所提供的代碼不足以編譯和運行。 在ContactCard.java

String conName; 
List<String> conPhoneType; 
List<String> conPhoneValue; 
List<String> conEmailType; 
List<String> conEmailValue; 
List<ContactCardModel> listConCard = new ArrayList<ContactCardModel>(); 

int i = 1; 

    for (AddressBookContact addressBookContact : list) { 

     conName = addressBookContact.name; 

     conPhoneType = new ArrayList<String>(); 
     conPhoneValue = new ArrayList<String>(); 
     if(addressBookContact.phones != null) { 

      for (int x = 0; x < addressBookContact.phones.size(); x++) { 
       int type = (int) addressBookContact.phones.keyAt(x); 
       String typeName = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(addressBookContact.res, type, ""); 
       String phValue = addressBookContact.phones.valueAt(x); 

       conPhoneType.add(typeName); 
       conPhoneValue.add(phValue); 
      } 
     } 

     conEmailType = new ArrayList<String>(); 
     conEmailValue = new ArrayList<String>(); 
     if(addressBookContact.emails != null){ 

      for(int x = 0; x < addressBookContact.emails.size(); x++){ 
       int type = (int) addressBookContact.emails.keyAt(x); 
       String typeName = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(addressBookContact.res, type, ""); 
       String emailValue = addressBookContact.emails.valueAt(x); 

       conEmailType.add(typeName); 
       conEmailValue.add(emailValue); 
      } 
     } 

     listConCard.add(new ContactCardModel(conName, conPhoneType, conPhoneValue, conEmailType, conEmailValue)); 

    } 

在ContactCardModel.java

public ContactCardModel(String mConName, List<String> mConPhoneType, List<String> mConPhoneValue, 
        List<String> mConEmailType, List<String> mConEmailValue){ 

this.conName = mConName; 
this.conPhoneType = mConPhoneType; 
this.conPhoneValue = mConPhoneValue; 
this.conEmailType = mConEmailType; 
this.conEmailValue = mConEmailValue; 

}

+0

確定的構造函數。讓我運行代碼 –

+0

謝謝@kishor。現在我得到了預期的產出。 –

+0

永遠歡迎! –

相關問題