Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
public class MySpinnerActivity extends Activity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spCitys);
spinner.setAdapter(new MyAdapter(this));//要绑定的适配器,上面spinner要显示的数据及样子全部由MyAdapter来处理
}
}
public class MyAdapter extends BaseAdapter{
private List citys;
private Context context;
public MyAdapter(Context context) {
citys = new ArrayList();//此处数据源一般是从网络上得的,此处直接写一些死数据
citys.add("上海");
citys.add("北京");
citys.add("武汉");
this.context = context;
}
@Override
public int getCount() {//有几条数据呢?
return citys.size();
}
@Override
public Object getItem(int position) {//当前这一项是什么呢
return citys.get(position);
}
@Override
public long getItemId(int position) {//这一项的位置对应的ID是什么
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {//如果要绑定还有界面的样式,就得重写
//int position,所点击的成员索引,convertView,当前的控件,指的是就小界面,此处是main_item.xml
//如何加载不是本页面设定的界面layout(在A界面上加载B界面上的控件)
//布局加载器
LayoutInflater infalter = LayoutInflater.from(context);//实例化
//加载
convertView = infalter.inflate(R.layout.main_item, null);//加载要设置信息的小界面
//怎么实例化imageview,textview
ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
TextView tv = (TextView)convertView.findViewById(R.id.textView1);
//设置图片和数据
tv.setText(citys.get(position).toString());
iv.setImageResource(R.drawable.w);
return convertView;
}
}
今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现)
(一):使用ArrayAdapter进行适配数据:
①:首先定义一个布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
< 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下来列表中进行显示:
1
2
3
4
5
6
7
8
9
|
< 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中加入如下的代码(使用了系统定义的下拉列表的布局文件,当然也可以自定义)
1
2
3
4
5
6
7
8
|
// 初始化控件 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的点击事件(效果图如上图):
1
2
3
4
5
6
7
8
9
10
11
12
|
mSpinner.setOnItemSelectedListener( new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String str=parent.getItemAtPosition(position).toString(); Toast.makeText(SpinnerActivity. this , "你点击的是:" +str, 2000 ).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); |
(二)使用自定义的Adapter(重点)
①:定义每一个Item的布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<? 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 > |
②:建立Person类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.jiangqq.csdn; public class Person { private String personName; private String personAddress; public Person(String personName, String personAddress) { super (); this .personName = personName; this .personAddress = personAddress; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this .personName = personName; } public String getPersonAddress() { return personAddress; } public void setPersonAddress(String personAddress) { this .personAddress = personAddress; } } |
③:创建MyAdapter继承与BaseAdapter,进行适配:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.jiangqq.csdn; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * 自定义适配器类 * @author jiangqq <a href=http://blog.csdn.net/jiangqq781931404></a> * */ public class MyAdapter extends BaseAdapter { private List<Person> mList; private Context mContext; public MyAdapter(Context pContext, List<Person> 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 ) { TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1); TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2); _TextView1.setText(mList.get(position).getPersonName()); _TextView2.setText(mList.get(position).getPersonAddress()); } return convertView; } } |
④:在Activity中加入如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
// 初始化控件 mSpinner = (Spinner) findViewById(R.id.spinner1); // 建立数据源 List<Person> persons= new ArrayList<Person>(); persons.add( new Person( "张三" , "上海 " )); persons.add( new Person( "李四" , "上海 " )); persons.add( new Person( "王五" , "北京" )); persons.add( new Person( "赵六" , "广州 " )); // 建立Adapter绑定数据源 MyAdapter _MyAdapter= new MyAdapter( this , persons); //绑定Adapter mSpinner.setAdapter(_MyAdapter); |
运行效果如下截图: