省份和城市的二级列表

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));

        }
    });



}


spProvince = (Spinner)findViewById(R.id.spAddMettingAddressProvince); spCity = (Spinner)findViewById(R.id.spAddMettingAddressCity); ArrayAdapter<CharSequence> aAdapter = ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item); aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spProvince.setAdapter(aAdapter); spProvince.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Spinner spinner = (Spinner)parent; String pro = (String)spinner.getItemAtPosition(position); ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.city_default, android.R.layout.simple_spinner_item); //new ArrayAdapter<CharSequence> // (MainActivity.this,android.R.layout.simple_spinner_item, cities); //获取所在省含有哪些市(从资源数组文件中获取数据) if(pro.equals("河北省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.hebei, android.R.layout.simple_spinner_item); }else if(pro.equals("北京市")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.bj, android.R.layout.simple_spinner_item); }else if(pro.equals("山西省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.shan1xi, android.R.layout.simple_spinner_item); }else if(pro.equals("天津市")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.tianjing, android.R.layout.simple_spinner_item); }else if(pro.equals("内蒙古")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.neimeng, android.R.layout.simple_spinner_item); }else if(pro.equals("辽宁省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.liaoning, android.R.layout.simple_spinner_item); }else if(pro.equals("吉林省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.jilin, android.R.layout.simple_spinner_item); }else if(pro.equals("黑龙江省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.heilongjjiang, android.R.layout.simple_spinner_item); }else if(pro.equals("上海市")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.shanghai, android.R.layout.simple_spinner_item); }else if(pro.equals("江苏省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.jiangsu, android.R.layout.simple_spinner_item); }else if(pro.equals("浙江省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.zhejiang, android.R.layout.simple_spinner_item); }else if(pro.equals("安徽省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.anhui, android.R.layout.simple_spinner_item); }else if(pro.equals("福建省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.fujian, android.R.layout.simple_spinner_item); }else if(pro.equals("江西省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.jiangxi, android.R.layout.simple_spinner_item); }else if(pro.equals("山东省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.shandong, android.R.layout.simple_spinner_item); }else if(pro.equals("河南省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.henan, android.R.layout.simple_spinner_item); }else if(pro.equals("湖北省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.hubei, android.R.layout.simple_spinner_item); }else if(pro.equals("湖南省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.hunan, android.R.layout.simple_spinner_item); }else if(pro.equals("广东省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.guangdong, android.R.layout.simple_spinner_item); }else if(pro.equals("广西自治区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.guangxi, android.R.layout.simple_spinner_item); }else if(pro.equals("海南省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.hainan, android.R.layout.simple_spinner_item); }else if(pro.equals("重庆市")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.chongqing, android.R.layout.simple_spinner_item); }else if(pro.equals("四川省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.sichuan, android.R.layout.simple_spinner_item); }else if(pro.equals("贵州省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.guzhou, android.R.layout.simple_spinner_item); }else if(pro.equals("云南省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.yunan, android.R.layout.simple_spinner_item); }else if(pro.equals("西藏自治区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.xizang, android.R.layout.simple_spinner_item); }else if(pro.equals("陕西省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.shan3xi, android.R.layout.simple_spinner_item); }else if(pro.equals("甘肃省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.gansu, android.R.layout.simple_spinner_item); }else if(pro.equals("青海省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.qinghai, android.R.layout.simple_spinner_item); }else if(pro.equals("宁夏回族自治区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.ningxia, android.R.layout.simple_spinner_item); }else if(pro.equals("新疆维吾尔自治区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.xinjiang, android.R.layout.simple_spinner_item); }else if(pro.equals("香港特别行政区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.xianggang, android.R.layout.simple_spinner_item); }else if(pro.equals("澳门特别行政区")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.aomen, android.R.layout.simple_spinner_item); }else if(pro.equals("台湾省")){ cityAdapter = ArrayAdapter.createFromResource (MettingAddActivity.this, R.array.taiwan, android.R.layout.simple_spinner_item); } cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //绑定数据到Spinner(City)上 spCity.setAdapter(cityAdapter); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值