码迷,mamicode.com
首页 > 其他好文 > 详细

如何借助ArrayAdapter和Spinner实现下拉列表

时间:2015-01-12 19:13:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

        这两天研究了一下利用ArrayAdapter和Spinner实现下拉列表的功能,现将代码与大家共享:

        代码1—添加名为“Area.java”的文件:

package com.ghj.vo;

import java.io.Serializable;

public class Area implements Serializable {

	private static final long serialVersionUID = 6262838923156998909L;
	private String id;
	private String name;

	public Area(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return name;
	}
}
        注意:上面Area类重写了toString方法技术分享

        代码2—添加名为“SelectActivity.java”的文件:

package com.ghj.activity;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.ghj.vo.Area;

public class SelectActivity extends Activity {

	private Spinner selectSpinner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select);
		selectSpinner = (Spinner) findViewById(R.id.spinner);
		List<Area> areaList = getSpinnerData();
		ArrayAdapter<Area> adapter = new ArrayAdapter<Area>(this, android.R.layout.simple_spinner_item, areaList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		selectSpinner.setAdapter(adapter);
		
		setDefaultSpinner(areaList, "07bb7787-29eb-4c83-a781-e4fdfff398f5");//设置默认选中项
		
		selectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Area area = (Area) selectSpinner.getSelectedItem();
				Toast.makeText(SelectActivity.this, area.getId() + "→" + area.getName(), Toast.LENGTH_LONG).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {				
			}
		});
	}
	
	/**
	 * 设置下拉列表默认选中项
	 * 
	 * @author 高焕杰
	 */
	private void setDefaultSpinner(List<Area> areaList, String id){
		for (int i = 0; i < areaList.size(); i++) {
			if(id.equals(areaList.get(i).getId())){
				selectSpinner.setSelection(i,true);
				break;
			}
		}
	}
	
	private List<Area> getSpinnerData(){
		List<Area> areaList = new ArrayList<Area>();
		areaList.add(new Area("40c73928-7cfc-4830-9e38-e019e0637400", "天津"));
		areaList.add(new Area("b1ce40fa-d79d-4e2a-82e0-7ef8ca90bdf5", "上海"));
		areaList.add(new Area("0888b0af-b7a6-4fbb-a400-0f3050372f1d", "重庆"));
		areaList.add(new Area("32a308c8-f985-42b6-bf92-45c5723b95ed", "北京"));
		areaList.add(new Area("07bb7787-29eb-4c83-a781-e4fdfff398f5", "浙江省"));
		areaList.add(new Area("18618a4a-55e7-4028-b757-dc2c0fc07d25", "江苏省"));
		areaList.add(new Area("1ebf214f-09eb-4700-907f-a4ecb05859c4", "辽宁省"));
		areaList.add(new Area("3792f25c-2e69-423a-9a3c-6b798595f6e0", "湖南省"));
		areaList.add(new Area("3ed3babe-03ab-43b7-9600-3c57ae3ce73c", "贵州省"));
		areaList.add(new Area("5866f512-5b59-495a-8db9-e8fb3ef3f8b3", "台湾省"));
		areaList.add(new Area("8819d96d-006b-4eb4-aef8-5ece3c65a1ce", "福建省"));
		areaList.add(new Area("888d4f82-a350-461e-bc18-f5910097c8f7", "四川省"));
		areaList.add(new Area("93d45f23-0898-4d61-b3b0-f3ea116755bf", "云南省"));
		areaList.add(new Area("afb8aa76-c4fb-4b03-beb3-be1f2b5452b5", "吉林省"));
		areaList.add(new Area("b3d5f316-286e-4a9b-959d-8d62a5dc3a85", "湖北省"));
		areaList.add(new Area("b42bf54b-c625-4d50-8368-5d5b2771781c", "甘肃省"));
		areaList.add(new Area("b53544bf-68ed-4899-b41b-9ad8fbfbf5d2", "河北省"));
		areaList.add(new Area("be7d1660-7d7d-43c4-a2f0-b90e982c6e69", "海南省"));
		areaList.add(new Area("d74167e5-546e-46b8-b561-5dfdba5a8c0b", "山西省"));
		areaList.add(new Area("db5c6c99-02f8-4a63-95fa-3ee1fe99bedb", "河南省"));
		areaList.add(new Area("dc0a7c06-83fb-425b-a4b2-1382bde1edc0", "山东省"));
		areaList.add(new Area("e21f5930-7bb4-4853-bed0-282e91ef8b43", "江西省"));
		areaList.add(new Area("e634c6bd-c3a1-474c-b9e1-542a6cb21f1d", "安徽省"));
		areaList.add(new Area("e6a7fc64-483c-4646-a5da-a505e7eb1188", "广东省"));
		areaList.add(new Area("f5d74601-d950-4dfa-ab2b-92419b7acb9d", "陕西省"));
		areaList.add(new Area("f5f666fe-8adc-4f41-9e7d-7bfecc71fcd3", "青海省"));
		areaList.add(new Area("94d8d374-75d7-4fd9-ba2b-861c07633625", "黑龙江省"));
		areaList.add(new Area("09f576c9-f2d7-4c7f-a3bd-885c4e233a72", "西藏自治区"));
		areaList.add(new Area("ea3d0b1f-7834-40b5-aed7-e6b2230afac7", "内蒙古自治区"));
		areaList.add(new Area("1599f19b-205c-4ac7-9340-b13d41715871", "广西壮族自治区"));
		areaList.add(new Area("450e6119-2921-445e-8d0e-b345b9c71f3f", "香港特别行政区"));
		areaList.add(new Area("992afa93-c9c0-4f1d-b8ce-cafd35f06358", "宁夏回族自治区"));
		areaList.add(new Area("f6b4b94c-0506-469d-bcc1-437cb3c2ae70", "澳门特别行政区"));
		areaList.add(new Area("0ff240ce-df6c-49c8-83ec-837667b6a748", "新疆维吾尔自治区"));
		return areaList;
	}
}

        代码3—添加名为“activity_select.xml”的文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

        代码4—修改名为“styles.xml”的文件:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="android:Theme.Light">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

        【0分下载示例

如何借助ArrayAdapter和Spinner实现下拉列表

标签:

原文地址:http://blog.csdn.net/gaohuanjie/article/details/42646195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!