ITEEDU

Spinner下拉列表

主要是数据绑定
 
MainActivity.java

package com.iteedu.spinner;

import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		find_and_modify_view();
	}

	Spinner spinner_c;
	Spinner spinner_2;
	private ArrayAdapter aspnCountries;
	private List allcountries;
	//用于spanner01的数据
	private static final String[] mCountries = { 
		"China", "Russia", "Germany",
			"Ukraine", "Belarus", "USA" };

	private void find_and_modify_view() {
		//绑定spanner01
		spinner_c = (Spinner) findViewById(R.id.Spinner01);
		allcountries = new ArrayList();
		for (int i = 0; i < mCountries.length; i++) {
			allcountries.add(mCountries[i]);
		}
		aspnCountries = new ArrayAdapter(this,
			android.R.layout.simple_spinner_item, allcountries);
		aspnCountries.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		spinner_c.setAdapter(aspnCountries);
		
		//绑定spanner02
		spinner_2 = (Spinner) findViewById(R.id.Spinner02);
        ArrayAdapter adapter = 
        	ArrayAdapter.createFromResource(
                this, R.array.countries, 
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(
        		android.R.layout.simple_spinner_dropdown_item);
        spinner_2.setAdapter(adapter);
	}
}


array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<!-- Used in Spinner/spinner_2.java -->
	<string-array name="countries">
		<item>China2</item>
		<item>Russia2</item>
		<item>Germany2</item>
		<item>Ukraine2</item>
		<item>Belarus2</item>
		<item>USA2</item>
	</string-array>
</resources>