有关于在下拉列表Spinner中添加自定义文字和图标的方法,这里以人的姓名和头像,地址为例,当然只要作出修改就可以把适配器应用于ListView 显示。
首先创建一个储存人信息的Person类。
public class Person {
private String personName, personAddress;
private Integer drawable;
public Person(String personName,String personAddress,Integer drawable){
super();
this.personName = personName;
this.personAddress = personAddress;
this.drawable = drawable;
}
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;
}
public Integer getDrawable() {
return drawable;
}
public void setDrawable(Integer drawable) {
this.drawable = drawable;
}
}
然后创建一个MyAdapter类作为适配器继承自BaseAdapter,重写必须的四个方法
public class MyAdapter extends BaseAdapter{
private List mList = null;
private Context mContext = null;
public MyAdapter(Context pContext,List 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) {
//利用getView方法给我们提供的convertView,也可以自己新建一个Layout
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext); //布局加载权限
convertView = mLayoutInflater.inflate(R.layout.item_custom, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
imageView.setImageResource(mList.get(position).getDrawable());
TextView mTextView1 = (TextView) convertView.findViewById(R.id.textView1);
TextView mTextView2 = (TextView) convertView.findViewById(R.id.textView2);
mTextView1.setText(mList.get(position).getPersonName());
mTextView2.setText(mList.get(position).getPersonAddress());
return convertView;
}
}
还有自定义的布局文件
最后是Activity类的内容,这里建立两个Spinner作为对比,一个是简单使用系统的ArrayAdapter,另一个则是我们自定义的adapter
public class MainActivity extends ActionBarActivity {
private Spinner spinner1 = null;
private Spinner spinner2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
String[] mItems = getResources().getStringArray(R.array.language);
ArrayAdapter adapter = new ArrayAdapter
(this,android.R.layout.simple_spinner_item,mItems); //绑定字符串数组
spinner1.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setSelection(0, true); //默认选中第一个
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
String[] language = getResources().getStringArray(R.array.language);
Toast.makeText(MainActivity.this, "你点击的是:" + language[position], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
final List persons = new ArrayList(); //建立数据源
persons.add(new Person("张三","北京",R.drawable.beijing)); //添加数据
persons.add(new Person("李四", "上海",R.drawable.shanghai));
persons.add(new Person("王五", "广州",R.drawable.guangzhou));
persons.add(new Person("赵六", "深圳",R.drawable.shenzhen));
MyAdapter myAdapter = new MyAdapter(this,persons); //实例化适配器
spinner2.setAdapter(myAdapter); //下拉列表绑定适配器
spinner2.setSelection(0,true); //默认选中第一个,避免打开程序就立即执行onItemSelected方法
//设置点击列表项目监听事件
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "你点击的是:" +
persons.get(position).getPersonName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
}
}