Android控件之Spinner

ArrayAdapter


<span style="font-size:18px;">// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 建立数据源
String[] mItems = getResources().getStringArray(R.array.spinnername);
// 建立Adapter并且绑定数据源
ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
//绑定 Adapter到控件
mSpinner.setAdapter(_Adapter);</span>


自定义Adapter

创建每一项的布局文件

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/item_linearlayout"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/tv_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        
    </LinearLayout>

</LinearLayout></span>

创建数据源

<span style="font-size:18px;">package com.example.spinnertest;

public class Student {
	private String name;
	private String sex;
	private int age;

	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}</span>

自定义适配器

<span style="font-size:18px;">package com.example.spinnertest;

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.LinearLayout;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private List<Student> studentList;
	private Context context;

	public MyAdapter(List<Student> studentList, Context context) {
		super();
		this.studentList = studentList;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return studentList.size();
	}

	@Override
	public Student getItem(int position) {
		// TODO Auto-generated method stub
		return studentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout linearLayout = null;
		if (convertView != null) {
			linearLayout = (LinearLayout) convertView;
		} else {
			linearLayout = (LinearLayout) LayoutInflater.from(context).inflate(
					R.layout.activity_item, null);
			TextView tv_Name=(TextView) linearLayout.findViewById(R.id.tv_large);
			TextView tv_Sex=(TextView) linearLayout.findViewById(R.id.tv_small);
			tv_Name.setText(studentList.get(position).getName());
			tv_Sex.setText(studentList.get(position).getSex());
		}
		return linearLayout;
	}

}</span>

绑定数据并添加监听事件

<span style="font-size:18px;">package com.example.spinnertest;

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.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class MainActivity extends Activity {
	private Spinner spinner;
	private List<Student> students = new ArrayList<Student>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinner);

		this.spinner = (Spinner) findViewById(R.id.view_spinner);
		students.add(new Student("张三", "男", 18));
		students.add(new Student("李四", "男", 20));
		students.add(new Student("王五", "女", 19));
		MyAdapter adapter = new MyAdapter(students, this);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this,
						students.get(position).getName(), Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	}
}</span><span style="font-size:24px;">
</span>

运行结果示意



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值