冰室辰也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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示