• Spinner simpleAdapte适配器 下拉列表


    public class MainActivity extends AppCompatActivity {
        private TextView text;
        private Spinner spinner;
    
        private SimpleAdapter adapter;
        private List<Map<String,Object>> data;
    
        private String[] place = {"北京","上海","深圳","佛山"};
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //注册控件
            text = (TextView)findViewById(R.id.txt);
            spinner=(Spinner)findViewById(R.id.spinner);
    
            //数据源
            data = new ArrayList<Map<String,Object>>();
            getdata();
    
            //适配器
            adapter = new SimpleAdapter(this,data,R.layout.item,new String[]{"pic","txt"},new int[]{R.id.picture,R.id.name});
            adapter.setDropDownViewResource(R.layout.item);
    
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    text.setText("您选择的是:" + adapter.getItem(position));
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    text.setText("None");
                }
            });
        }
        private void getdata() {
            for(int i= 0; i < place.length; i++){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("pic",R.drawable.address_book);
                map.put("txt",place[i]);
                data.add(map);
            }
        }
  • 相关阅读:
    存储结构接收数组
    oracle数据库sql根据查看执行计划优化sql--走不走索引
    多线程--Thread
    java常用集合族谱
    设计模式之二 适配模式
    Tomcat优化问题
    设计模式之一
    C++虚函数表,虚表指针,内存分布
    设计模式
    linux环境下的时间编程
  • 原文地址:https://www.cnblogs.com/zmaibbs7/p/4842930.html
Copyright © 2020-2023  润新知