i3geek.com
闫庚哲的个人博客

Android之Spinner(下拉框)的使用

一、使用数组作为数据源(使用ArrayAdapter进行适配数据)

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

public class SpinnerActivity extends Activity {

	private static final String[] m={"A型","B型","O型","AB型","其他"};
	private TextView view ;
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);

		view = (TextView) findViewById(R.id.spinnerText);
		spinner = (Spinner) findViewById(R.id.Spinner01);
		//将可选内容与ArrayAdapter连接起来
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);

		//设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		//将adapter 添加到spinner中
		spinner.setAdapter(adapter);

		//添加事件Spinner事件监听  
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());

		//设置默认值
		spinner.setVisibility(View.VISIBLE);

	}

	//使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener{

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			view.setText("你的血型是:"+m[arg2]);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
}

二、使用XML作为数据源 (Spinner的UI格式)

在values文件夹下新建一个arryas.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="plantes">
		<item>NOKIA</item>
		<item>MOTO</item>
		<item>HTC</item>
		<item>LG</item>
		<item>其他</item>
	</string-array>
</resources>

 

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

public class SpinnerActivity extends Activity {

	private TextView view2;
	private Spinner spinner2;
	private ArrayAdapter adapter2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);

		spinner2 = (Spinner) findViewById(R.id.spinner02);
		view2 = (TextView) findViewById(R.id.spinnerText02);

		//将可选内容与ArrayAdapter连接起来
		adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);

		//设置下拉列表的风格	
 adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		//将adapter2 添加到spinner中
		spinner2.setAdapter(adapter2);

		//添加事件Spinner事件监听  
		spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());

		//设置默认值
		spinner2.setVisibility(View.VISIBLE);

	}

	//使用XML形式操作
	class SpinnerXMLSelectedListener implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			view2.setText("你使用什么样的手机:"+adapter2.getItem(arg2));
		}

		public void onNothingSelected(AdapterView<?> arg0) {

		}

	}
}

三、使用自定义的Adapter(自定义格式和数据)

①建立一个自定义类。(这里以Person类为例)

package com.jiangqq.csdn;
public class Person {
	private String personName;
	private String personAddress;
	public Person(String personName, String personAddress) {
		super();
		this.personName = personName;
		this.personAddress = personAddress;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public String getPersonAddress() {
		return personAddress;
	}
	public void setPersonAddress(String personAddress) {
		this.personAddress = personAddress;
	}

}

②创建MyAdapter继承与BaseAdapter,进行适配:

package com.jiangqq.csdn;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 自定义适配器类
 * @author jiangqq  <a href=http://blog.csdn.net/jiangqq781931404></a>
 *
 */
public class MyAdapter extends BaseAdapter {
	private List<Person> mList;
	private Context mContext;

	public MyAdapter(Context pContext, List<Person> pList) {
		this.mContext = pContext;
		this.mList = pList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
    /**
     * 下面是重要代码
     */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
		convertView=_LayoutInflater.inflate(R.layout.item, null);
		if(convertView!=null)
		{
			TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1);
			TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2);
			_TextView1.setText(mList.get(position).getPersonName());
			_TextView2.setText(mList.get(position).getPersonAddress());
		}
		return convertView;
	}
}

③在Activity中加入如下代码:

                // 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 建立数据源
   List<Person>  persons=new ArrayList<Person>();
persons.add(new Person("张三", "上海 "));
persons.add(new Person("李四", "上海 "));
persons.add(new Person("王五", "北京" ));
persons.add(new Person("赵六", "广州 "));
//  建立Adapter绑定数据源
MyAdapter _MyAdapter=new MyAdapter(this, persons);
//绑定Adapter
mSpinner.setAdapter(_MyAdapter);

四、使用list链表作为数据源

public class WuliuMain extends Activity
{
	private List<String> list = new ArrayList<String>();
	private Spinner mySpinner;
	private ArrayAdapter<String> adapter;

	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wuliu_main);

		list.add("ems快递");
		list.add("申通");
		list.add("顺丰");
		mySpinner = (Spinner) findViewById(R.id.spinner1);
                adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mySpinner.setAdapter(adapter);
	}
}

 

赞(0)
未经允许不得转载:爱上极客 » Android之Spinner(下拉框)的使用
分享到: 更多 (0)

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    路过来学习一下,多谢博主分享

    heyuan5年前 (2014-02-08)回复
  2. #2

    学习了,呵呵

    Dzing5年前 (2014-02-08)回复