楼主遇到的问题跟图片上一样。Webservice接口有两个方法:方法一是无参数的,直接返回一级列表,包含名字和ID;方法二是根据第一个所返回的ID返回二级列表。
题主现在要实现点一级项目,会展开二级项目,而且这个二级项目的列表不是提前存好的,是通过点击一级列表之后传参数给方法二得到的。
楼主现在能想到的代码如下,请大神帮忙改改,关键就在于“childList = ByGetLocation.getCityList("parentId:81");”这一句的处理,因为81是可变的
public class LocationActivity extends Activity {
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
expandableListView = (ExpandableListView) findViewById(R.id.elv_location);
List<ByGetLocation> groupList = new ArrayList<ByGetLocation>();
List<ByGetLocation> childList = new ArrayList<ByGetLocation>();
groupList = ByGetLocation.getProvince();
childList = ByGetLocation.getCityList("parentId:81");
ExpandableListAdapter adapter = new ExpandableListAdapter(groupList, childList,this);
expandableListView.setAdapter(adapter);
}
private class ExpandableListAdapter extends BaseExpandableListAdapter{
private List<ByGetLocation> groupList = null;
private List<ByGetLocation> childList = null;
private Context context;
public ExpandableListAdapter(List<ByGetLocation> groupList,
List<ByGetLocation> childList, Context context) {
super();
this.groupList = groupList;
this.childList = childList;
this.context = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int GroupPosition, int ChildPosition, boolean isLastChild,
View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
ChildViewHolder childViewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_elv_child, null);
childViewHolder = new ChildViewHolder();
childViewHolder.childText = (TextView) convertView.findViewById(R.id.elv_city_name);
convertView.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.childText.setText(childList.get(ChildPosition).CityName);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupList.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupList.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int GroupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
GroupViewHolder groupViewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_elv_group, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.groupText = (TextView) convertView.findViewById(R.id.elv_province_name);
convertView.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.groupText.setText(groupList.get(GroupPosition).CityName.toString());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
private class GroupViewHolder{
TextView groupText;
}
private class ChildViewHolder{
TextView childText;
}
}
}