模型类对象列表中的数据无法正确检索?

时间:2016-11-08 08:31:18

标签: java android arraylist android-contentprovider

当我尝试从listConCard(在我的活动类ContactCard.class中)中检索单个对象时," conName"所有联系人卡都正确接收,但我没有从其兄弟阵列获得正确的数据" conPhoneType"," conPhoneValue"," conEmailType"," conEmailValue&# 34。

listConCard是我的模型类ContactCardModel.class的对象列表。

我正在添加" ContactCardModel" listConCard中的对象如下

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

并检索" ContactCardModel"的数据对象如下

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

}

两个数组的值getConPhoneType和getConPhoneValue都返回0个元素

两个数组的值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;
}

 }

1 个答案:

答案 0 :(得分: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;

}