模型 class 的对象列表中的数据未正确检索?
Data from list of objects of model class is not retrieving correctly?
当我尝试从 listConCard 中检索单个对象时(在我的 activity class ContactCard.class 中),"conName" 对于所有 contactCards 都正确接收但我没有从其兄弟数组 "conPhoneType"、"conPhoneValue"、"conEmailType"、"conEmailValue".
获取正确数据
listConCard 是我的模型的对象列表 class 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;
}
当我尝试从 listConCard 中检索单个对象时(在我的 activity class ContactCard.class 中),"conName" 对于所有 contactCards 都正确接收但我没有从其兄弟数组 "conPhoneType"、"conPhoneValue"、"conEmailType"、"conEmailValue".
获取正确数据listConCard 是我的模型的对象列表 class 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;
}