如何借助SimpleAdapter和Spinner实现下拉列表
来源:互联网 发布:linux查看nvidia显卡 编辑:程序博客网 时间:2024/06/17 12:59
这两天研究了一下利用SimpleAdapter和Spinner实现下拉列表的功能,现将代码与大家共享:
代码1—添加名为“SelectActivity.java”的文件:
package com.ghj.activity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.TextView;import android.widget.Toast;public class SelectActivity extends Activity {private Spinner spinner;private SimpleAdapter simpleAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.select);spinner = (Spinner) findViewById(R.id.spinner);List<Map<String, String>> specialtyList = getSpinnerData();simpleAdapter = new SimpleAdapter(this, specialtyList, R.layout.select_item, new String[]{"id","name"}, new int[]{R.id.value, R.id.lable});spinner.setAdapter(simpleAdapter);setDefaultSpinner(specialtyList, "yingyu");//设置默认选中项spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {String lable = ((TextView) SelectActivity.this.findViewById(R.id.lable)).getText().toString();String value = ((TextView) SelectActivity.this.findViewById(R.id.value)).getText().toString();Toast.makeText(SelectActivity.this, lable + "→" + value, Toast.LENGTH_LONG).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}/** * 设置下拉列表默认选中项 * * @author 高焕杰 */private void setDefaultSpinner(List<Map<String, String>> mapList, String id){for (int i = 0; i < mapList.size(); i++) {Map<String, String> map = mapList.get(i);if(id.equals(map.get("id"))){spinner.setSelection(i,true);break;}}}private List<Map<String, String>> getSpinnerData(){List<Map<String, String>> specialtyList = new ArrayList<Map<String, String>>();Map<String, String> map1 = new HashMap<String, String>();map1.put("id", "yuwen");map1.put("name", "语文");specialtyList.add(map1);Map<String, String> map2 = new HashMap<String, String>();map2.put("id", "shuxue");map2.put("name", "数学");specialtyList.add(map2);Map<String, String> map3 = new HashMap<String, String>();map3.put("id", "yingyu");map3.put("name", "英语");specialtyList.add(map3);return specialtyList;}}
代码2—添加名为“select_item_selector.xml”的文件:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#60FFA500" /> </shape> </item> <item android:state_focused="true"> <shape> <solid android:color="#60FFA500" /> </shape></item></selector>
代码3—添加名为“select.xml”的文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>
代码4—添加名为“select_item.xml”的文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/select_item_selector" android:orientation="vertical" > <TextView android:id="@+id/lable" android:layout_width="fill_parent" android:layout_height="45dp" android:gravity="center_vertical" android:paddingLeft="12dp" /> <TextView android:id="@+id/value" android:layout_width="wrap_content" android:layout_height="match_parent" android:visibility="gone" /></LinearLayout>
代码5—修改文件名为“styles.xml”的文件:
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="android:Theme.Light"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style></resources>【0分下载示例】
0 0
- 如何借助SimpleAdapter和Spinner实现下拉列表
- 如何借助ArrayAdapter和Spinner实现下拉列表
- Spinner(实现下拉列表)
- spinner实现下拉列表
- ArrayAdapter 实现下拉列表Spinner
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 使用Spinner实现下拉列表
- 关于下拉列表的实现(spinner和PopupWindow+listview)
- Dialog实现Spinner下拉列表效果
- 使用RelativeLayout实现自定义下拉列表Spinner
- spinner实现下拉列表中选择项目
- Android学习--Spinner实现下拉列表
- Android:控件Spinner实现下拉列表
- Android+Jquery Mobile学习系列(6)-个人信息设置
- 利用xlrd处理Excel文件
- 数据库启动与状态监测
- 数据结构 课程设计 字符串加密器
- xcode6建一个empty工程
- 如何借助SimpleAdapter和Spinner实现下拉列表
- 利用python的csv库处理csv文件
- java restful 后端 Exception 的处理架构总结
- 最好用Unity3D按住鼠标键才转动摄像机的方法
- numpy 中 + 和 += 的区别
- 提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- uva10308(树)
- clustring testing -- robin canopy
- TaskAffinity