Android中的省,市,区,实现三级联动

android中的省,市,区,实现三级联动

通常我们会在买票的时候用到查询市,或者旅游时用到地区的查询,都会涉及到。。。。。。

下面二话不说直接贴代码。

这里写代码片
xml布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_Province"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="省份" />

        <TextView
            android:id="@+id/tv_City"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="城市" />

        <TextView
            android:id="@+id/tv_Zone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="区域" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/lv_Province"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ListView
            android:id="@+id/lv_City"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ListView
            android:id="@+id/lv_Zone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
这里写代码片
item布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/mtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="城市"
        android:gravity="center"
         />

</LinearLayout>
这里写代码片
两个javaBean类
其一Province类
package com.example.cn.bgs.jsonsspdemo;

import java.util.List;

public class Province {
    private List<City> cityList;
    private String provinceName;

    public List<City> getCityList() {
        return cityList;
    }
    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }
    public String getProvinceName() {
        return provinceName;
    }
    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

}
这里写代码片
其二City类
package com.example.cn.bgs.jsonsspdemo;

import java.util.List;

public class City {
    private List<String> zoneList;
    private String cityName;

    public List<String> getZoneList() {
        return zoneList;
    }
    public void setZoneList(List<String> zoneList) {
        this.zoneList = zoneList;
    }
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

}

这里写代码片
MyAdapter代码
package com.example.cn.bgs.jsonsspdemo;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> list;

    public MyAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public void refresh(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public T getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item, null);
            holder.mtv = (TextView) convertView.findViewById(R.id.mtv);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        T t = list.get(position);
        if (t instanceof Province) {
            Province p = (Province) t;
            holder.mtv.setText(p.getProvinceName());
        }
        if (t instanceof City) {
            City c = (City) t;
            holder.mtv.setText(c.getCityName());
        }
        if (t instanceof String) {
            String zone = (String) t;
            holder.mtv.setText(zone);
        }
        return convertView;
    }

    class ViewHolder {
        TextView mtv;
    }
}
这里写代码片
MainActivity代码
package com.example.cn.bgs.jsonsspdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tv_Province, tv_City, tv_Zone;
    private ListView lv_Province, lv_City, lv_Zone;
    private MyAdapter proadapter;
    private MyAdapter cityadapter;
    private MyAdapter zoneadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        String s = getData();
        List<Province> proList = getJson(s);
        proadapter = new MyAdapter<Province>(MainActivity.this, proList);
        lv_Province.setAdapter(proadapter);
        lv_Province.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Province p = (Province) proadapter.getItem(position);
                tv_Province.setText(p.getProvinceName());
                tv_City.setText("市");
                tv_Zone.setText("区");
                List<City> cList = p.getCityList();
                if (cityadapter == null) {
                    cityadapter = new MyAdapter<City>(MainActivity.this, cList);
                    lv_City.setAdapter(cityadapter);
                } else {
                    cityadapter.refresh(cList);
                }
                if (zoneadapter != null) {
                    zoneadapter.refresh(null);
                }
            }
        });
        lv_City.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                City c = (City) cityadapter.getItem(position);
                List<String> zoneList = c.getZoneList();
                tv_City.setText(c.getCityName());
                if (zoneadapter == null) {
                    zoneadapter = new MyAdapter<String>(MainActivity.this,
                            zoneList);
                    lv_Zone.setAdapter(zoneadapter);
                } else {
                    zoneadapter.refresh(zoneList);
                }
            }
        });
        lv_Zone.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String string = (String) zoneadapter.getItem(position);
                tv_Zone.setText(string);
            }
        });
    }

    private void initView() {
        tv_Province = (TextView) findViewById(R.id.tv_Province);
        tv_City = (TextView) findViewById(R.id.tv_City);
        tv_Zone = (TextView) findViewById(R.id.tv_Zone);

        lv_Province = (ListView) findViewById(R.id.lv_Province);
        lv_City = (ListView) findViewById(R.id.lv_City);
        lv_Zone = (ListView) findViewById(R.id.lv_Zone);
    }

    public String getData() {

        try {
            AssetManager asset = getResources().getAssets();
            InputStream in = asset.open("ssq.txt");
            InputStreamReader reader = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(reader);
            StringBuffer buffer = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                buffer.append(line);

            }
            br.close();

            return buffer.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public List<Province> getJson(String s) {
        try {
            List<Province> proList = new ArrayList<Province>();
            JSONArray ja = new JSONArray(s);
            for (int i = 0; i < ja.length(); i++) {
                Province p = new Province();
                JSONObject jb = ja.getJSONObject(i);
                String proName = jb.getString("name");
                JSONArray jacity = jb.getJSONArray("city");
                List<City> cityList = new ArrayList<City>();
                for (int j = 0; j < jacity.length(); j++) {
                    City c = new City();
                    JSONObject jbb = jacity.getJSONObject(j);
                    String cityName = jbb.getString("name");
                    JSONArray jazone = jbb.getJSONArray("area");

                    List<String> zoneList = new ArrayList<String>();
                    for (int k = 0; k < jazone.length(); k++) {
                        String zoneName = jazone.getString(k);
                        zoneList.add(zoneName);
                    }
                    c.setZoneList(zoneList);
                    c.setCityName(cityName);
                    cityList.add(c);

                }
                p.setProvinceName(proName);
                p.setCityList(cityList);
                proList.add(p);

            }

            return proList;

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

}

这里写代码片
assets文件夹
ssq.txt文件

代码都写完了,下面看一下效果图:
这里写图片描述

好了,一切都结束了,谢谢。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是阿亮啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值