2016-11-08 11 views
2

(私のアクティビティクラスContactCard.classの)listConCardから単一のオブジェクトを取得しようとすると、すべてのcontactCardの "conName"が正しく受信しますが、兄弟配列 "conPhoneType"、 "conPhoneValue" "conEmailType"、 "conEmailValue"。モデルクラスのオブジェクトのリストからのデータが正しく取得されていませんか?

listConCardは、私のモデルクラスContactCardModel.classのオブジェクトのリストです。

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

と両方の配列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の要素を返すされている次のように "ContactCardModel" オブジェクトのデータを取得し、次のようにiがlistConCardに "ContactCardModel" オブジェクトを追加してい

両方の配列の値getConEmailTypeおよびgetConEmailValueが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

以下の改変をしてみてください。提供されたコードがコンパイルと実行に十分でないため、コードを実行してテストすることができません。 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

OKのコンストラクタにおいて

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)); } 

ContactCard.java

で。コードを実行させてください –

+0

ありがとう@kishor。今私は期待された出力を得ています。 –

+0

いつもようこそ! –