我已经创建了一个可扩展的列表适配器,我正在尝试访问我传入的对象(例如id)的属性(CoolObject)
问题是我传递给列表适配器的头(在listDataHeader中)可能与另一个头相同,但显然有不同的对象属性。我该如何解决这个问题?
例如this._listDataHeader
可能包含'CoolObjectOne','CoolObjectOne'。如果我尝试getChild
,它将返回第一个对象的属性(例如id),而不管groupPosition / childPosition
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<CoolObject>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<CoolObject>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition,
final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.live_final_list_item, null);
}
CoolObject co = (CoolObject) getChild(groupPosition, childPosition); // this is where the duplicate heading issue is
// return cool object name
coolObjectName.setText(lv.getName());
// return cool object id
coolObjectId.setText(lv.getId());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
try {
return this._listDataHeader.get(groupPosition);
} catch (Exception e) {
e.getMessage();
return "";
}
}
@Override
public int getGroupCount() {
try {
return this._listDataHeader.size();
} catch (Exception e) {
return 0;
}
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.competition_list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
CoolObject.java
public class CoolObject {
// constructor
public CoolObject(String name, String id) {
this.id = id;
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}