Spinner--下拉菜單

private TextView textView;
        private Spinner spinner;
        private List<String>list;
        private ArrayAdapter<String>arrayAdapter;

        textView = (TextView)findViewById(R.id.spinner_textView);
        spinner = (Spinner)findViewById(R.id.spinner);
        //1.設置數據源
        list = new ArrayList<String>();
        list.add("北京");
        list.add("上海");
        list.add("廣州");
        list.add("深圳");
        //2.新建數組適配器
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
        //3.適配器設置一個下拉列表樣式
        arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        //4.spinner加載適配器(ArrayAdapter,SimpleAdapter均可以)
        spinner.setAdapter(arrayAdapter);
        //5,監聽器
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String cityname = arrayAdapter.getItem(position);
//              String cityname = list.get(position);

                textView.setText(cityname);
            }

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

            }
        });
相關文章
相關標籤/搜索