原ArrayAdapter适配器列表样式只支持TextView,要自定义列表项,因此自定义一个适配器 :
1.创建自定义类
2.创建这自定义类的ArrayList数组
3.创建自定义适配器, 重写适配器构造函数将该数组作为输入参数 ;重写自定义适配器中的getView() 方法
4.创建自定义XML列表布局
5.ListView设置其适配器
以Miwok项目为例, 具体项目Example见http://github.com/uxk0587/miwok.git
1.创建自定义类
//自定义一个类
public class Word {
private String miwokTranslation;
private String defaultTranslation;
private int imageResourceId;
private int audioResourceId;
public Word(String miwokTranslation, String defaultTranslation){
this.miwokTranslation = miwokTranslation;
this.defaultTranslation = defaultTranslation;
}
public Word(String miwokTranslation, String defaultTranslation, int audioResourceId){
this.miwokTranslation = miwokTranslation;
this.defaultTranslation = defaultTranslation;
this.audioResourceId =audioResourceId;
}
public Word(String miwokTranslation, String defaultTranslation, int imageResourceId, int audioResourceId) {
this.miwokTranslation = miwokTranslation;
this.defaultTranslation = defaultTranslation;
this.imageResourceId = imageResourceId;
this.audioResourceId = audioResourceId;
}
public String getMiwokTranslation(){
return miwokTranslation;
}
public String getDefaultTranslation(){
return defaultTranslation;
}
public int getImageResourceId(){
return imageResourceId;
}
public int getAudioResourceId(){
return audioResourceId;
}
public boolean checkImageResource(){
if (imageResourceId == 0) {
return false;
} else
return true;
}
}