天天看點

android 中spinner 三級關聯

java代碼如下

package com.zz.spinner;

import android.app.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.zz.spinner.bean.ThreeEntity;
import com.zz.spinner.bean.TwoEntity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
    private Spinner oneSpinner;
    private Spinner twoSpinner;
    private Spinner threeSpinner;
    private EditText editResult;

    /**一級集合*/
    private List<String> oneList = new ArrayList<String>();

    /**二級集合*/
    private List<TwoEntity> twoList = new ArrayList<TwoEntity>();

    /**三級集合*/
    private List<ThreeEntity> threeList = new ArrayList<ThreeEntity>();

    /**預設集合*/
    private List<String> indexList ;

    /**擴充卡*/
    private ArrayAdapter<String> adapter;

    /**一級名稱*/
    private String oneName;

    /**二級名稱*/
    private String twoName;

    /**三級名稱*/
    private String threeName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        // 初始化資料
        adapterData(oneList,oneSpinner);
        Log.i("zhouzhuo","日志列印了啊=====");

    }

    private void initData() {
        ///增加一級資料
        oneList.add("山東");
        oneList.add("河北");
        oneList.add("河南");


        //添加二級資料
        TwoEntity two = new TwoEntity();

        two.setTwoName("濟南");
        two.setParentName("山東");

        TwoEntity two3 = new TwoEntity();

        two3.setTwoName("青島");
        two3.setParentName("山東");

        TwoEntity two4 = new TwoEntity();

        two4.setTwoName("德州");
        two4.setParentName("山東");

        TwoEntity two1 = new TwoEntity();

        two1.setTwoName("石家莊");
        two1.setParentName("河北");

        TwoEntity two5 = new TwoEntity();

        two5.setTwoName("保定");
        two5.setParentName("河北");

        TwoEntity two2 = new TwoEntity();

        two2.setTwoName("鄭州");
        two2.setParentName("河南");

        twoList.add(two);
        twoList.add(two1);
        twoList.add(two2);
        twoList.add(two3);
        twoList.add(two4);
        twoList.add(two5);


        //添加三級資料
        ThreeEntity three = new ThreeEntity();


        three.setTwoName("章丘");
        three.setParentName("濟南");

        ThreeEntity three1 = new ThreeEntity();

        three1.setTwoName("城陽");
        three1.setParentName("青島");

        ThreeEntity three2 = new ThreeEntity();
        three2.setTwoName("齊河");
        three2.setParentName("德州");

        ThreeEntity three3 = new ThreeEntity();
        three3.setTwoName("長安");
        three3.setParentName("石家莊");

        ThreeEntity three4 = new ThreeEntity();
        three4.setTwoName("清苑");
        three4.setParentName("保定");

        ThreeEntity three5 = new ThreeEntity();
        three5.setTwoName("登封");
        three5.setParentName("鄭州");

        threeList.add(three);
        threeList.add(three1);
        threeList.add(three2);
        threeList.add(three3);
        threeList.add(three4);
        threeList.add(three5);
    }


    private void initView() {
        oneSpinner = (Spinner) findViewById(R.id.one_spinner);
        twoSpinner = (Spinner) findViewById(R.id.two_spinner);
        threeSpinner = (Spinner) findViewById(R.id.three_spinner);
        oneSpinner.setOnItemSelectedListener(this);
        twoSpinner.setOnItemSelectedListener(this);
        threeSpinner.setOnItemSelectedListener(this);

        editResult = (EditText) findViewById(R.id.display_edit);


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (parent.getId()){
                case R.id.one_spinner://一級清單
                    Toast.makeText(this,"點選了一級清單",Toast.LENGTH_SHORT).show();
                    oneName = oneSpinner.getItemAtPosition(position).toString();
                    //  找到相應的二級資料
                    selectTwoData(oneName);

                    //适配二級資料
                    adapterData(indexList,twoSpinner);
                    break;
                case R.id.two_spinner://二級清單
                    Toast.makeText(this,"點選了二級清單",Toast.LENGTH_SHORT).show();
                    twoName = twoSpinner.getItemAtPosition(position).toString();

                    //找到相應的三級資料
                    selectThreeData(twoName);

                    //适配三級資料
                    adapterData(indexList,threeSpinner);
                    break;
                case R.id.three_spinner://  三級清單
                    threeName =threeSpinner.getItemAtPosition(position).toString();
                    editResult.setText(oneName+" "+twoName+" "+threeName);


                    break;

            }
    }




    /***
     * 找到相應的三級資料
     * @param twoName
     */

    private void selectThreeData(String twoName) {
        indexList = new ArrayList<String>();
        // 循環三級清單
        for(int i=0;i<threeList.size();i++){
            if(twoName.equals(threeList.get(i).getParentName())){
                indexList.add(threeList.get(i).getTwoName());
            }
        }


     }

    private void adapterData(List<String> indexList, Spinner twoSpinner) {
        Log.i("zhouzhuo","indexlist size:"+indexList.size());

            adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,indexList);
            //設定下拉清單風格
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //将 adapter設定到spinner中
            twoSpinner.setAdapter(adapter);
            //twoSpinner.setVisibility(View.VISIBLE);

    }

    /**
     *
     * public void onNothingSelected(AdapterView<?> parent) {
     oneSpinner.setSelection(0);
     twoSpinner.setSelection(0);
     threeSpinner.setSelection(0);
     }
     * @param parent
     */
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Log.i("zhouzhuo","onNothingSelected");
        oneSpinner.setSelection(0);
        twoSpinner.setSelection(0);
        threeSpinner.setSelection(0);
    }

    /***
     * 找到相應的二級資料
     * @param oneName
     */
    private void selectTwoData(String oneName) {
        indexList = new ArrayList<String>();
        //循環三級資料集合,找到符合條件的資料
        for(int i=0;i<twoList.size();i++){
            if(oneName.equals(twoList.get(i).getParentName())){
                indexList.add(twoList.get(i).getTwoName());
            }
        }

    }
}
      

xml  檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="false"
    >
    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="20px"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10px"
            android:scrollbarAlwaysDrawVerticalTrack="false" >

            <TextView
                android:id="@+id/province_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="省份:" >
            </TextView>

            <Spinner
                android:id="@+id/one_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </Spinner>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10px"
            android:scrollbarAlwaysDrawVerticalTrack="false" >

            <TextView
                android:id="@+id/city_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="城市:" >
            </TextView>

            <Spinner
                android:id="@+id/two_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </Spinner>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10px"
            android:scrollbarAlwaysDrawVerticalTrack="false" >

            <TextView
                android:id="@+id/county_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="縣城鎮:" >
            </TextView>

            <Spinner
                android:id="@+id/three_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </Spinner>
        </TableRow>

        <EditText
            android:id="@+id/display_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            android:hint="輸出結果"
            android:paddingTop="10px" >
        </EditText>
    </TableLayout>

</RelativeLayout>