让ListView更强大的BaseAdapter
- 上一篇文章中的例子我们使用了ArrayAdapter作为数据与ListView的桥梁,完成将数据加载到ListView的功能。但展示出来的列表(子项)数据显得太过单调,没有一个子项该有的复杂。例如我们想要一个子项显示一个联系人信息,那它必然包含头像、姓名、联系电话等详细信息。那我们如何实现这一功能呢,答案很明显,我们需要用到BaseAdapter来自定义一个适配器。
BaseAdapter是什么?
-
BaseAdapter是Android应用程序中经常用到的基础数据适配器的基类,它实现了Adapter接口。它可以将一组数据传到ListView显示组件进行显示。使用BaseAdapter主要是通过继承此类来实现BaseAdapter的四个方法:
public int getCount(): 适配器中数据集的数据个数。
public Object getItem(int position): 获取数据集中与索引对应的数据项。
public long getItemId(int position): 获取指定行对应的ID。
public View getView(int position,View convertView,ViewGroup parent): 获取每一行Item的显示内容。
利用BaseAdapter完成简单通讯录
- 我们准备完成的功能:
- 创建自定适配器MyAdapter继承自BaseAdapter,重写上述四个方法。在getView()方法中绑定布局,设置内容。
- 在主活动页面显示已定义好联系人数据。
- 可以手动增加联系人到列表中。
- 创建自定义适配器MyAdapter。
关键代码如下:
public class MyAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private Context mcontext;
private List<Person> personlist;
public MyAdapter (Context context, List<Person> list) {
layoutInflater = LayoutInflater.from(context);
this.mcontext = context;
this.personlist = list;
}
@Override
public int getCount() {
return personlist.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.person_information, null);
}
else{
view = convertView;
}
//绑定布局
TextView textView = (TextView)view.findViewById(R.id.text_name);
TextView textView1 = (TextView)view.findViewById(R.id.text_phonenumber);
ImageView imageView = (ImageView)view.findViewById(R.id.image);
//设置内容
textView.setText(personlist.get(position).getName());
textView1.setText(personlist.get(position).getPhonenum());
imageView.setImageResource(personlist.get(position).getImageId());
return view;
}
}
一些说明
-
public MyAdapter (Context context, List<Person> list)
这是MyAdapter的有参构造方法,用于获取获取context和list对象。其中第一个参数传入上下文,第二个参数传入适配数据集合,这里我们传入一个List集合(其中包含联系人数据)。
LayoutInflater是自定义适配器常用的一个类,它的作用类似于findViewById()。不同点是LayoutInflater是用来找layout下的xml布局文件,并且实例化。 -
public View getView(int position, View convertView, ViewGroup parent)
用来获取每一行Item所需的数据。
- 第二个功能和第三个功能我们放在一起实现,他们都定义在MainActivity中。
定义一些初始化联系人数据,代码如下:
private void getData() {
Person p1 = new Person("jjj", "223253", R.drawable.banana_pic);
Person p2 = new Person("高老板", "12534315", R.drawable.apple_pic);
Person p3 = new Person("蛤", "23454353", R.drawable.mango_pic);
Person p4 = new Person("hk", "223245342", R.drawable.strawberry_pic);
Person p5 = new Person("齐", "46543", R.drawable.pear_pic);
Person p6 = new Person("ppn", "456546", R.drawable.banana_pic);
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
personList.add(p5);
personList.add(p6);
}
点击按钮,添加联系人到ListView列表中,代码如下:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText1.getText().toString();
String number = editText2.getText().toString();
Person p7 = new Person(name, number, R.drawable.pineapple_pic);
personList.add(p7);
}
});
一些说明
-
Person p1 = new Person("jjj", "223253", R.drawable.banana_pic);
Person是我们自定义的一个类,用于存储联系人姓名、电话号码、头像ID。它的具体代码在最后给出。 -
personList.add(p1);
personlist是实例化的一个集合,通过add()方法可以将每个联系人对象添加到此集合中,用于传参。
我们来看一看具体实现效果
-
初始界面如下:
初始化界面 -
添加联系人成功后:
添加联系人
Github地址
- 主布局代码
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/res/layout/activity_main.xml - 子项布局代码
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/res/layout/person_information.xml - 主活动代码
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/MainActivity.java - 自定义适配器代码
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/MyAdapter.java - 自定义Person类代码
https://github.com/0xmxhnc/AndroidTest/blob/master/AddressBook/app/src/main/java/com/example/addressbook/Person.java