package drawerlayouts.com.xliscom.administrator.fengran20160313.adapters; import android.content.Context; import android.database.DataSetObserver; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.TextView; import java.util.List; import drawerlayouts.com.xliscom.administrator.fengran20160313.R; import drawerlayouts.com.xliscom.administrator.fengran20160313.bean.ListBean; import drawerlayouts.com.xliscom.administrator.fengran20160313.bean.MyCitys; /** * Created by Administrator on 2017/3/13. */ public class MyAdapters extends BaseExpandableListAdapter { private List<ListBean> list; private Context context; public MyAdapters(List<ListBean> list, Context context) { this.list = list; this.context = context; } private int parentCount; private int childCount; @Override public int getGroupCount() { return list.size(); } @Override public int getChildrenCount(int groupPosition) { return list.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return list.get(groupPosition).getCity(); } @Override public Object getChild(int groupPosition, int childPosition) { return list.get(groupPosition).getList().get(childPosition).getCity(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.paretens, null); } TextView textView = (TextView) convertView.findViewById(R.id.tv_parents); textView.setText(list.get(groupPosition).getCity()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.item_child, null); } TextView textView = (TextView) convertView.findViewById(R.id.t1); textView.setText(list.get(groupPosition).getList().get(childPosition).getCity()); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }package drawerlayouts.com.xliscom.administrator.fengran20160313.bean; import java.util.List; /** * Created by aoc on 2017/3/13. */ public class ListBean { private String city; private List<MyCitys.ResultBean> list; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public List<MyCitys.ResultBean> getList() { return list; } public void setList(List<MyCitys.ResultBean> list) { this.list = list; } }private void getdata() { OkHttpUtils.get().url(str).build().execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { } @Override public void onResponse(String response, int id) { Gson gson = new Gson(); MyCitys cityBean = gson.fromJson(response, MyCitys.class); List<ListBean> list1 = new ArrayList<>(); //Map<String, List<CityBean.ResultBean>> listMap = new HashMap<>(); for (MyCitys.ResultBean resultBean : cityBean.getResult()) { if (resultBean.getParentid().equals("0")) { ListBean listBean = new ListBean(); List<MyCitys.ResultBean> list2 = new ArrayList<>(); for (MyCitys.ResultBean bean : cityBean.getResult()) { if (bean.getParentid().equals(resultBean.getCityid())) { list2.add(bean); } } listBean.setCity(resultBean.getCity()); listBean.setList(list2); list1.add(listBean); } } elv.setAdapter(new MyAdapters( list1,MainActivity.this)); } }); }
省份和城市的二级列表
最新推荐文章于 2019-08-09 16:40:02 发布