在上一篇博客 中我演示了如何使用ListView来显示一个字符串(原始类型)数组,本篇博客继续讨论ListView,但这次要实现一个类的数组。
要想显示一个类的数组,需要有这么几个步骤:
- 定义一个类用于存储数据
- 添加一个用于显示每一项数据的Layout
- 实现一个自定义的Adapter,对数组中的每一个对象都要:inflate这个Layout,并将对象的信息显示到layout上
- 将这个adapter对象赋值给一个ListView控件
假设我们需要显示一个学生信息表,下面是Student类
public class Student {
private String name;
private String gender;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
添加一个layout,取名为student_layout.xml,下面是代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="3"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewName"
android:layout_columnWeight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewGender"
android:layout_columnWeight="1"/>
<LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewAge"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="岁"/>
</LinearLayout>
</GridLayout>
想要实现一个类的列表,ArrayAdapter已经不能满足我们了,我们需要自定义一个Adapter,但这个类可以继承自ArrayAdapter,也可以继承自BaseAdapter,因为我们要显示一个ArrayList<Student>
类型的数组,所以可以直接继承自ArrayAdapter。接下来新建一个名叫StudentAdapter的类,代码如下:
public class StudentAdapter extends ArrayAdapter<Student> {
public StudentAdapter(Context context, int resource, List<Student> items){
super(context,resource,items);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
if(v == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
v = inflater.inflate(R.layout.student_layout,null);
}
Student student = getItem(position);
if(student != null){
TextView textViewName = (TextView) v.findViewById(R.id.textViewName);
TextView textViewGender = (TextView) v.findViewById(R.id.textViewGender);
TextView textViewAge = (TextView) v.findViewById(R.id.textViewAge);
textViewName.setText(student.getName());
textViewGender.setText(student.getGender());
textViewAge.setText(student.getAge() + "");
}
return v;
}
}
接下来,activity_main.xml中加入一个ListView,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zdk.complexlistview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="students"
android:id="@+id/textView1"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:id="@+id/listView1"/>
</RelativeLayout>
接下来,要初始化一个数组,实例化一个StudentAdapter对象并指定为ListView的adapter。下面是MainActivity类的代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
List<Student> studentList = new ArrayList<>();
Student std1 = new Student();
std1.setName("lucy");
std1.setAge(16);
std1.setGender("女");
Student std2 = new Student();
std2.setName("lilei");
std2.setAge(16);
std2.setGender("男");
Student std3 = new Student();
std3.setName("hanmeimei");
std3.setAge(16);
std3.setGender("女");
studentList.add(std1);
studentList.add(std2);
studentList.add(std3);
StudentAdapter adapter = new StudentAdapter(this,R.layout.student_layout,studentList);
listView1.setAdapter(adapter);
}
}
大功告成!