冰室辰也hl 2016-07-21 15:05 采纳率: 50%
浏览 1438
已采纳

ExpandableListView分两级添加数据如何做?

图片说明
楼主遇到的问题跟图片上一样。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;
        }

    }

}
  • 写回答

1条回答 默认 最新

  • 冰室辰也hl 2016-07-22 02:49
    关注

    求助!!!!!!!!!!!!!!!!!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档