程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> adapter-ExpandableListView分兩級添加數據如何做?

adapter-ExpandableListView分兩級添加數據如何做?

編輯:編程解疑
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved