Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓通過spinner實現二級選擇地區(根據思路可以寫三級)

安卓通過spinner實現二級選擇地區(根據思路可以寫三級)

編輯:關於Android編程

思路I :通過二維數據放我們需要的城市地區 ,第一個spinner加載第一級城市,監聽第一級城市去控制第二個spinner加載二級城市。

 

代碼如下:

MainActivity

 

package com.example.testdiqu;

import java.util.ArrayList;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class MainActivity extends ActionBarActivity {
	private Spinner parent,chaird;
	private ArrayList list=new ArrayList();//城市
	private ArrayList clist=new ArrayList();//子城市
    private String [][] citys=new String[][]{
    		{濟南市,歷下區,市中區,槐蔭區,天橋區,歷城區,長清區,平陰縣,濟陽縣,商河縣,章丘市},
    		{青島市,市南區,市北區,四方區,黃島區,崂山區,李滄區,城陽區,膠州市,即墨市,平度市,膠南市,萊西市},
    		{淄博市,淄川區,張店區,博山區,臨淄區,周村區,桓台縣,高青縣,沂源縣},
    		{棗莊市,薛城區,峄城區,台兒莊區,山亭區,滕州市},
    		{東營市,東營區,河口區,墾利縣,利津縣,廣饒縣},
    		{濰坊市,濰城區,寒亭區,坊子區,奎文區,臨朐縣,昌樂縣,青州市,諸城市,壽光市,安丘市,高密市,昌邑市},
    		{煙台市,芝罘區,福山區,牟平區,萊山區,長島縣,龍口市,萊陽市,萊州市,蓬萊市,招遠市,棲霞市,海陽市},
    		{威海市,環翠區,文登市,榮成市,乳山市},
    		{濟寧市,任城區,微山縣,魚台縣,金鄉縣,嘉祥縣,汶上縣,泗水縣,梁山縣,曲阜市,兖州市,鄒城市},	
    		{泰安市,泰山區,岱岳區,寧陽縣,東平縣,新泰市,肥城市},
    		{日照市,東港區,岚山區,五蓮縣,莒縣},
    		{萊蕪市,萊城區,鋼城區},
    		{臨沂市,蘭山區,羅莊區,沂南縣,郯城縣,沂水縣,蒼山縣,費縣,平邑縣},
    		{德州市,德城區,寧津縣,慶雲縣,臨邑縣,齊河縣,平原縣,夏津縣,武城縣,樂陵市,禹城市,莒南縣,蒙陰縣,臨沭縣},
    		{聊城市,東昌府區,陽谷縣,莘縣,茌平縣,東阿縣,冠縣,高唐縣,臨清市},
    		{濱州市,濱城區,惠民縣,陽信縣,無棣縣,沾化縣,博興縣,鄒平縣},
    		{菏澤市,牡丹區,曹縣,單縣,巨野縣,定陶縣,成武縣,郓城縣,鄄城縣,東明縣}	
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parent=(Spinner)findViewById(R.id.parent);
		chaird=(Spinner)findViewById(R.id.chaird);
		loadData();
    }
    private void loadData() {
		// TODO Auto-generated method stub
		for(int i=0;i adapter=new ArrayAdapter
		(MainActivity.this, android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		parent.setAdapter(adapter);
		parent.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				    clist.clear();//清除數據
					for(int j=1;j adapter=new ArrayAdapter
					(MainActivity.this, android.R.layout.simple_spinner_item, clist);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				chaird.setAdapter(adapter);
			}

			@Override
			public void onNothingSelected(AdapterView parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

main_activity.xml

 

 



    

    




 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved