当我试图从listconcard(在我的活动类contactcard.class中)检索单个对象时,所有contactcard的“conname”都会正确接收,但我无法从其同级数组“conphonetype”、“conphonevalue”、“conemailtype”、“conemailvalue”中获取正确的数据。
listconcard是我的模型类contactcardmodel.class的对象列表。
我正在listconcard中添加“contactcardmodel”对象,如下所示
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和getconemailvalues数组的值都返回1个元素
conphoneType和conphoneValue的大小相同
conemailtype和conemailvalue的大小相同
联系人卡.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;
}
}
最佳答案
尝试下面的修改。无法运行和测试代码,因为提供的代码不足以编译和运行。
在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;
}