gpt4 book ai didi

java - 模型类对象列表中的数据未正确检索?

转载 作者:搜寻专家 更新时间:2023-11-01 09:39:50 25 4
gpt4 key购买 nike

当我尝试从 listConCard(在我的 Activity 类 ContactCard.class 中)检索单个对象时,所有 contactCards 的“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 和 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;
}

}

最佳答案

尝试以下修改。无法运行和测试代码,因为提供的代码不足以编译和运行。在 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;

关于java - 模型类对象列表中的数据未正确检索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com