我想ExpandableListView
从解析的 JSON 文件中填充一个。
目标是将第一个条目 (kontakt_titel) 作为组的标题,将所有其他条目作为子项...
JSON 文件如下所示:
{
"contact":[
{
"kontakt_titel":"Berlin",
"adresse_strasse":"Hauptstrasse 1",
"adresse_plzort":"4000 Berlin",
"telefon":"123456789",
"fax":"123456780",
"email":"berlin@123.com",
"website":"www.berlin.123.com"
},
{
"kontakt_titel":"London",
"adresse_strasse":"East Highway 1",
"adresse_plzort":"London",
"telefon":"123456789",
"fax":"123456780",
"email":"london@123.com",
"website":"www.london.123.com"
},
{
"kontakt_titel":"New York",
"adresse_strasse":"Time Square 1",
"adresse_plzort":"12345",
"telefon":"123456789",
"fax":"123456780",
"email":"newyork@123.com",
"website":"www.newyork.123.com"
}
]
}
这是我想将这些元素解析为 ExpandableListView 的代码:
try {
JSONArray jContact = json.getJSONArray("contact");
for (int i = 0; i < jContact.length(); i++) {
JSONObject c4 = jContact.getJSONObject(i);
String contact_title = c4.getString("kontakt_titel");
String adresse_strasse = c4.getString("adresse_strasse");
String adresse_plzort = c4.getString("adresse_plzort");
String telefon = c4.getString("telefon");
String fax = c4.getString("fax");
String email = c4.getString("email");
String website = c4.getString("website");
if (contact_title != null && !contact_title.equals("")) {
contactTitles.add(contact_title);
} else {
contactTitles.add("");
}
if (adresse_strasse != null && !adresse_strasse.equals("")) {
contactAddressStreet.add(adresse_strasse);
} else {
contactAddressStreet.add("");
}
if (adresse_plzort != null && !adresse_plzort.equals("")) {
contactAddressPlzOrt.add(adresse_plzort);
} else {
contactAddressPlzOrt.add("");
}
if (telefon != null && !telefon.equals("")) {
contactTelephone.add(telefon);
} else {
contactTelephone.add("");
}
if (fax != null && !fax.equals("")) {
contactFax.add(fax);
} else {
contactFax.add("");
}
if (email != null && !email.equals("")) {
contactEmail.add(email);
} else {
contactEmail.add("");
}
if (website != null && !website.equals("")) {
contactWebsite.add(website);
} else {
contactWebsite.add("");
}
}
} catch (JSONException e) {
errorHappened = true;
} catch (NullPointerException npe) {
errorHappened = true;
}
我认为这一切正常......但我认为这是我的错误......这是getChildView()
我的适配器扩展的方法BaseExpandableListAdapter
:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_contact, null);
}
TextView tvPlayerName = (TextView) convertView
.findViewById(R.id.tvPlayerName);
switch (childPosition) {
case 0:
tvPlayerName
.setText(MainActivity.contactAddressStreet.get(childPosition));
break;
case 1:
tvPlayerName
.setText(MainActivity.contactAddressPlzOrt.get(childPosition));
break;
case 2:
tvPlayerName
.setText(MainActivity.contactTelephone.get(childPosition));
break;
case 3:
tvPlayerName
.setText(MainActivity.contactFax.get(childPosition));
break;
case 4:
tvPlayerName
.setText(MainActivity.contactEmail.get(childPosition));
break;
case 5:
tvPlayerName
.setText(MainActivity.contactWebsite.get(childPosition));
break;
case 6:
tvPlayerName
.setText(MainActivity.contactOpening.get(childPosition));
break;
}
return convertView;
}
问题是,对于所有组中的所有孩子,它总是采用相同的条目。它将所有儿童中的第一个条目(柏林)作为第一,所有儿童中的第二个条目(东一号公路)的第二个条目作为第二个,依此类推。