您的位置:首页 > 其它

如何借助SimpleAdapter和Spinner实现下拉列表

2015-01-12 18:47 316 查看
        这两天研究了一下利用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;

@Override
protected 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() {
@Override
public 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();
}

@Override
public 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分下载示例
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐