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

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
    关注

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

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

报告相同问题?

悬赏问题

  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码
  • ¥15 esp32烧录失败,具体情况在图片上
  • ¥15 selenium安装报错
  • ¥15 在node.js中无法安装yarn
  • ¥15 python程序修改