1、数组适配器(ArrayAdapter):使用时直接new一个就好
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.act_main_item,data);
第一个参数为当前activity,直接this就好;第二个参数为项资源文件,即layout文件夹中的文件;第三个参数为数据源。
2、简单适配器(SimpleAdapter):使用时直接new一个就好
SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.act_main_item,new String[]{"image","name"},new int[]{R.id.sp_main_item_img,R.id.tv_main_item_content});
第一个参数为当前activity,直接this就好;第二个参数为数据源;第三个参数为项资源文件,即layout文件夹中的文件;第三个参数为由数据源List
Book实体类代码:
public class Book {
private Integer id;
private String name;
private Integer image;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getImage() {
return image;
}
public void setImage(Integer image) {
this.image = image;
}
}
BookDao中的代码(用以提供数据源):
public class BookDAO {
public List list() {
List bookList = new ArrayList();
String[] names = {"愤怒的小鸟", "汤姆猫", "落汤鸡", "牛牛", "哈巴狗", "神龙", "烤鸭","小象", "美人鱼", "九尾狐"};
int[] images = {R.drawable.bird, R.drawable.cat, R.drawable.chicken,
R.drawable.cow, R.drawable.dog, R.drawable.dragon,
R.drawable.duck, R.drawable.elephant, R.drawable.fish,
R.drawable.fox};
Book b = null;
for (int i = 0; i < names.length; i++) {
b = new Book();
b.setId(i + 1);
b.setName(names[i]);
b.setImage(images[i]);
bookList.add(b);
}
return bookList;
}
}
注:int数组images为drawable里的图片
act_main_item2.xml里的代码(项资源文件):
MainActivity里的代码:
public class MainActivity extends AppCompatActivity {
private Spinner sp_main_select2;
private SimpleAdapter adapter3;
private List
3、自定义适配器:BaseAdapter是所有适配器类的父类,所以自定义适配器时要写一个类继承BaseAdapter,然后实现四个方法,四个方法的解析如下图: