Android中Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
掃描二維碼
隨時(shí)隨地手機(jī)看文章
1 :Android中Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn)) .
今天學(xué)習(xí)了Spinner組件,使用Spinner相當(dāng)于從下拉列表中選擇項(xiàng)目,下面演示一下Spinner的使用(分別使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
(一):使用ArrayAdapter進(jìn)行適配數(shù)據(jù):
①:首先定義一個(gè)布局文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
②:建立數(shù)據(jù)源,使用數(shù)組,這些數(shù)據(jù)將會(huì)在Spinner下來列表中進(jìn)行顯示:
③:接著在Activity中加入如下的代碼(使用了系統(tǒng)定義的下拉列表的布局文件,當(dāng)然也可以自定義)
// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 建立數(shù)據(jù)源
String[] mItems = getResources().getStringArray(R.array.spinnername);
// 建立Adapter并且綁定數(shù)據(jù)源
ArrayAdapter
//綁定 Adapter到控件
mSpinner.setAdapter(_Adapter);
下面是關(guān)于Spinner的點(diǎn)擊事件(效果圖如上圖):
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, "你點(diǎn)擊的是:"+str, 2000).show();
}
@Override
public void onNothingSelected(AdapterView parent) {
// TODO Auto-generated method stub
}
});
(二)使用自定義的Adapter(重點(diǎn))
①:定義每一個(gè)Item的布局文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
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" />
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" />
②:建立Person類:
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;
}
}
③:創(chuàng)建MyAdapter繼承與BaseAdapter,進(jìn)行適配:
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
*
*/
public class MyAdapter extends BaseAdapter {
private List
private Context mContext;
public MyAdapter(Context pContext, List
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中加入如下代碼:
// 初始化控件
mSpinner = (Spinner) findViewById(R.id.spinner1);
// 建立數(shù)據(jù)源
List
persons.add(new Person("張三", "上海 "));
persons.add(new Person("李四", "上海 "));
persons.add(new Person("王五", "北京" ));
persons.add(new Person("趙六", "廣州 "));
// 建立Adapter綁定數(shù)據(jù)源
MyAdapter _MyAdapter=new MyAdapter(this, persons);
//綁定Adapter
mSpinner.setAdapter(_MyAdapter);