如果要像这样传递多个字段,则需要创建对象类型的ArrayList。现在是哪个对象?像这样上课-
public class PersonData {
private String name, bloodType, type, dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBloodType() {
return bloodType;
}
public void setBloodType(String bloodType) {
this.bloodType = bloodType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}
在LocalJsonFileActivity中,像这样解析和存储数据-
ArrayList<PersonData> data = new ArrayList<PersonData>();
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
PersonData mPersonData = new PersonData();
mPersonData.setName(jsonObject.getString("name"));
mPersonData.setBloodType(jsonObject.getString("bloodtype"));
mPersonData.setType(jsonObject.getString("type"));
String series = jsonObject.getString("dob");
if (series.equals("December")) {
mPersonData.setDob(jsonObject.getString("dob"));
}
data.add(mPersonData);
}
使用自定义适配器或修改正在使用的适配器的构造函数以将数据作为类型
ArrayList<PersonData>
作为参数。
然后在适配器中使用这些值,如下所示-
holder.TextViewName.setText(data.get(position).getName());
holder.TextViewBloodType.setText(data.get(position).getBloodType());
holder.TextViewType.setText(data.get(position).getType());
holder.TextViewDOB.setText(data.get(position).getDob());
不要复制完整的代码,试着理解我是如何做到的,并在您的项目中实现它。您的实现在适配器中可能略有不同,这只是一个演示如何执行这些操作。