Search code examples
javaandroidarraylistandroid-contentprovider

Data from list of objects of model class is not retrieving correctly?


When i try to retrieve a single object from listConCard (in my activity class ContactCard.class), "conName" for all the contactCards receives correctly but i am not getting correct data from its sibling arrays "conPhoneType", "conPhoneValue", "conEmailType", "conEmailValue".

listConCard is list of objects of my model class ContactCardModel.class.

i am adding "ContactCardModel" objects in listConCard as follows

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

and retrieving data of "ContactCardModel" objects as follows

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

}

values of both arrays getConPhoneType and getConPhoneValue are returning 0 elements

values of both arrays getConEmailType and getConEmailValueare returning 1 element

size of conPhoneType and conPhoneValue are same

size of conEmailType and conEmailValue are same

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

 }

Solution

  • Try the below modification. Unable run and test the code as the code provided is not enough to compile and run. In 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));
    
        }
    

    In constructor of 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;
    

    }