• Android攻城狮spinner实现下拉列表


      1 package com.example.zy_spanner;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 
      8 import android.support.v7.app.ActionBarActivity;
      9 import android.support.v7.app.ActionBar;
     10 import android.support.v4.app.Fragment;
     11 import android.app.Activity;
     12 import android.os.Bundle;
     13 import android.view.LayoutInflater;
     14 import android.view.Menu;
     15 import android.view.MenuItem;
     16 import android.view.View;
     17 import android.view.ViewGroup;
     18 import android.widget.AdapterView;
     19 import android.widget.AdapterView.OnItemClickListener;
     20 import android.widget.AdapterView.OnItemSelectedListener;
     21 import android.widget.ArrayAdapter;
     22 import android.widget.SimpleAdapter;
     23 import android.widget.Spinner;
     24 import android.widget.TextView;
     25 import android.os.Build;
     26 
     27 public class MainActivity extends Activity implements OnItemSelectedListener {
     28     private TextView textView,textView2;
     29     private Spinner spinner,spinner2;
     30     private List<String> list;
     31     private ArrayAdapter<String> adapter;
     32     
     33     
     34     private SimpleAdapter adapter2;
     35     private List<Map<String, Object>> datalist;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.fragment_main);
     41         textView = (TextView) findViewById(R.id.textview1);
     42         textView2 = (TextView) findViewById(R.id.textview);
     43         textView.setText("您选择的城市是:重庆");
     44         textView2.setText("您选择的城市是:重庆");
     45         spinner = (Spinner) findViewById(R.id.spinner1);
     46         spinner2= (Spinner) findViewById(R.id.spinner);
     47         // 1.设置数据源
     48         list = new ArrayList<String>();
     49         list.add("重庆");
     50         list.add("北京");
     51         list.add("上海");
     52         list.add("广州");
     53         // 2.新建ArrayAdapter(数组适配器)
     54         adapter = new ArrayAdapter<String>(this,
     55                 android.R.layout.simple_spinner_item, list);
     56         // 3.adapter设置一个下拉列表
     57         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     58         // 4.spinner加载适配器
     59         spinner.setAdapter(adapter);
     60         // 5.Spinner设置监听器
     61         spinner.setOnItemSelectedListener(this);
     62 
     63         // --------------------------------------
     64         datalist = new ArrayList<Map<String, Object>>();
     65     
     66         
     67         adapter2 = new SimpleAdapter(this,     getDate(), R.layout.item,
     68                 new String[] { "image", "text" }, new int[] { R.id.imageView2,
     69                         R.id.textView2 });
     70 
     71         adapter2.setDropDownViewResource(R.layout.item);
     72         spinner2.setAdapter(adapter2);
     73         spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
     74 
     75             @Override
     76             public void onItemSelected(AdapterView<?> parent, View view,
     77                     int position, long id) {
     78                 // TODO Auto-generated method stub
     79                 textView2.setText("您选择的城市是:"+adapter2.getItem(position));
     80             }
     81 
     82             @Override
     83             public void onNothingSelected(AdapterView<?> parent) {
     84                 // TODO Auto-generated method stub
     85                 textView2.setText("none");
     86             }
     87         });
     88     }
     89 
     90     
     91     @Override
     92     public void onItemSelected(AdapterView<?> parent, View view, int position,
     93             long id) {
     94         // TODO Auto-generated method stub
     95         String cityName = adapter.getItem(position);
     96         // String cityName=list.get(position);
     97         textView.setText("您选择的城市是:" + cityName);
     98     }
     99 
    100     @Override
    101     public void onNothingSelected(AdapterView<?> parent) {
    102         // TODO Auto-generated method stub
    103 
    104     }
    105     
    106     
    107     
    108 private List<Map<String, Object>> getDate(){
    109     Map<String, Object> map=new HashMap<String, Object>();
    110     map.put("image", R.drawable.ic_launcher);
    111     map.put("text", "重庆");
    112     
    113     Map<String, Object> map1=new HashMap<String, Object>();
    114     map1.put("image", R.drawable.ic_launcher);
    115     map1.put("text", "上海");
    116     
    117     Map<String, Object> map2=new HashMap<String, Object>();
    118     map2.put("image", R.drawable.ic_launcher);
    119     map2.put("text", "北京");
    120     
    121     Map<String, Object> map3=new HashMap<String, Object>();
    122     map3.put("image", R.drawable.ic_launcher);
    123     map3.put("text", "广州");
    124     datalist.add(map);
    125     datalist.add(map1);
    126     datalist.add(map2);
    127     datalist.add(map3);
    128     return datalist;
    129 }
    130 
    131 }

     

  • 相关阅读:
    libhdfs 写新的c程序编译问题
    CDH4源码 eclipse导入问题
    安装CDH4遇到的问题
    [学习资料]Java多线程编程
    [properJavaRDP]在网页中实现远程连接
    【原创】Eclipse实现图形化界面插件vs4e
    【原创】源码角度分析Android的消息机制系列(四)——MessageQueue的工作原理
    【原创】源码角度分析Android的消息机制系列(六)——Handler的工作原理
    【原创】源码角度分析Android的消息机制系列(三)——ThreadLocal的工作原理
    【原创】源码角度分析Android的消息机制系列(五)——Looper的工作原理
  • 原文地址:https://www.cnblogs.com/my334420/p/6706211.html
Copyright © 2020-2023  润新知