关闭

下拉框

标签: Adapter下拉框android
58人阅读 评论(0) 收藏 举报
分类:


package com.example.spinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String[] start=null;
	private String[] target=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		start = this.getResources().getStringArray(R.array.citys_start);
		target = this.getResources().getStringArray(R.array.citys_target);
				

		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
		//ArrayAdapter adapter1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1, android.R.id.text1, start);			
		spinner1.setAdapter(new MyAdapter1(this,-1));
		spinner1.setSelection(2);

		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
		ArrayAdapter adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1, android.R.id.text1, target);		
		spinner2.setAdapter(adapter2);
		spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,long id) {		
				Toast.makeText(getApplicationContext(), target[pos],Toast.LENGTH_SHORT).show();				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	private class MyAdapter1 extends ArrayAdapter {
    
		private  LayoutInflater infalter;
		
		public MyAdapter1(Context context, int resource) {
			super(context, resource);
			infalter=LayoutInflater.from(context);
		}

		@Override
		public int getCount() {

			return start.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view = infalter.inflate(android.R.layout.simple_list_item_1,null);
			TextView text= (TextView) view.findViewById(android.R.id.text1);
			text.setText("始发地:"+start[position]);
			
			return view;
		}

		@Override
		public View getDropDownView(int position, View convertView,ViewGroup parent) {
			View view = infalter.inflate(android.R.layout.simple_list_item_1,null);
			TextView text= (TextView) view.findViewById(android.R.id.text1);
			text.setText("始发地:"+start[position]);
			
			return view;
		}
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.spinner.MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <string-array name = "citys_start">
        <item>北京</item>
        <item>成都</item>
        <item>广州</item>
        <item>上海</item>
        <item>西安</item>
        <item>新疆</item>
        <item>深圳</item>
        <item>美国</item>
        <item>韩国</item>
        <item>大千世界</item>
		<item>三千小世界</item>
	</string-array>
	
    <string-array name="citys_target">
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
    </string-array>
    
</resources>




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20363次
    • 积分:1111
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:20篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论