Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)

来源:互联网 发布:mac os 启动管理界面 编辑:程序博客网 时间:2024/06/05 14:36

(一):使用ArrayAdapter进行适配数据:

①:首先定义一个布局文件:

<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Spinner        android:id="@+id/spinner1"        android:layout_width="match_parent"        android:layout_height="wrap_content"      /></LinearLayout></span>

Spinner有两个属性:1:prompt是初始的时候,Spinner显示的数据,是一个引用类型; 2:entries是直接在xml布局文件中绑定数据源(可以不设置,即可以在Activity中动态绑定)


②:建立数据源,使用数组,这些数据将会在Spinner下来列表中进行显示:

<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="spinnername">        <item>北京</item>        <item>上海 </item>        <item>广州</item>        <item>深圳</item>    </string-array></resources></span>

③:接着在Activity中加入如下的代码(使用了系统定义的下拉列表的布局文件,当然也可以自定义)

// 初始化控件mSpinner = (Spinner) findViewById(R.id.spinner1);// 建立数据源String[] mItems = getResources().getStringArray(R.array.spinnername);// 建立Adapter并且绑定数据源ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);//绑定 Adapter到控件mSpinner.setAdapter(_Adapter);

以上代码初步完成,看下运行效果:
这里写图片描述

下面是关于Spinner的点击事件:

mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {    @Override    public void onItemSelected(AdapterView<?> parent, View view,            int position, long id) {         // TODO Auto-generated method stub    }    @Override    public void onNothingSelected(AdapterView<?> parent) {        // TODO Auto-generated method stub    }});

(二)使用自定义的Adapter(重点)

①:定义每一个Item的布局文件

<?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:orientation="horizontal" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/ic_launcher"        android:paddingRight="8dip"        android:paddingTop="8dip"        android:text="TextView"        android:textSize="25sp" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="8dip"        android:paddingTop="8dip"        android:text="TextView"        android:textSize="25sp" /></LinearLayout>

②:建立city类:

public class City {    // 建立Adapter并且绑定数据源    private String city;    public String getCity() {        return city;    }    public void setCity(String city) {        this.city = city;    }}

③:创建MyAdapter继承与BaseAdapter,进行适配:

/** * 自定义适配器类 * */public class MyAdapter extends BaseAdapter {    private List<City> mList;    private Context mContext;    public MyAdapter(Context pContext, List<City> pList) {        this.mContext = pContext;        this.mList = pList;    }    @Override    public int getCount() {        return mList.size();    }    @Override    public Object getItem(int position) {        return mList.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    /**     * 下面是重要代码     */    @Override    public View getView(int position, View convertView, ViewGroup parent) {        LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);        convertView=_LayoutInflater.inflate(R.layout.item, null);        if(convertView!=null){           Tv1=(TextView)convertView.findViewById(R.id.textView1);           Tv11.setText(mList.get(position).getPersonName());         }        return convertView;    }}

④:在Activity中加入如下代码:

// 初始化控件mSpinner = (Spinner) findViewById(R.id.spinner1);// 数据源(动态获取City类,数据源为 List<City>  city) //  建立Adapter绑定数据源MyAdapter _MyAdapter=new MyAdapter(this, city);//绑定AdaptermSpinner.setAdapter(_MyAdapter);
阅读全文
0 0
原创粉丝点击