代码改变世界

Spinner下拉列表控件

2014-04-04 21:48  kingshow  阅读(206)  评论(0编辑  收藏  举报

 

    Spinner控件用于显示一个下拉列表,改控件在装载数据的时候需要创建一个Adapter适配器对象。并在创建的Adapter对象过程中指定要装载的数据是数组或者是List对象的数据。

一、建立工程,如图

二、图片及xml文件

三、main.xml

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

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

四、item.xml

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

    <ImageView android:id="@+id/imageview" android:layout_width="60dp"
        android:layout_height="60dp" android:src="@drawable/icon"
        android:paddingLeft="10dp"></ImageView>
    <TextView android:id="@+id/textview" android:textColor="#000"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:textSize="16dp" android:gravity="center_vertical"
        android:paddingLeft="10dp"></TextView>
</LinearLayout>
View Code

五、Mainactivity.java

package com.study.spinner1;

import java.util.List;
import java.util.Map;

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

import com.android.adapter.MyAdapter;

public class MainActivity extends Activity {

    private Spinner spinner;
    private Spinner spinner2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinner = (Spinner) this.findViewById(R.id.spinner);
        List<String> list = MyAdapter.getData();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, list);
        spinner.setAdapter(adapter);

        spinner2 = (Spinner) this.findViewById(R.id.spinner2);
        // List<Map<String,Object>>
        List<Map<String, Object>> listmaps = MyAdapter.getListMaps();
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, listmaps,
                R.layout.item, new String[] { "ivLogo", "applicationName" },
                new int[] { R.id.imageview, R.id.textview });
        spinner2.setAdapter(simpleAdapter);
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String appName = ((Map<String, Object>) spinner2
                        .getItemAtPosition(position)).get("applicationName")
                        .toString();
                setTitle(appName);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
View Code

六、MyAdapter.java

package com.android.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.study.spinner1.R;

public class MyAdapter {

    public MyAdapter() {
        // TODO Auto-generated constructor stub
    }

    public static List<String> getData() {
        List<String> list = new ArrayList<String>();
        list.add("北京");
        list.add("上海");
        list.add("广州");
        return list;
    }

    public static List<Map<String, Object>> getListMaps() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ivLogo", R.drawable.calendar);
        map1.put("applicationName", "日历");

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ivLogo", R.drawable.eoemarket);
        map2.put("applicationName", "eoemarket客户端");
        list.add(map1);
        list.add(map2);
        return list;

    }
}
View Code

七、效果图