实现功能:使用ListView显示学生信息
Student.java
public class Student {
private String name;
private int age;
private String class_name;
public Student(String name,int age,String class_name){
this.name=name;
this.age=age;
this.class_name=class_name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getClass_name() {
return class_name;
}
}
显示ListView的每个item,student_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/student_name"
android:gravity="center"
android:layout_weight="1"
android:text="姓名"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/student_age"
android:gravity="center"
android:layout_weight="1"
android:text="年龄"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/student_class_name"
android:gravity="center"
android:layout_weight="1"
android:text="班级"
android:layout_width="0dp"
android:layout_height="50dp" />
</LinearLayout>
为ListView建适配器:StudentAdapter.java
public class StudentAdapter extends ArrayAdapter<Student>{
private int resourceId;
//重写父类的构造函数,将上下文、ListView子项布局的id和数据都传递进来
public StudentAdapter(@NonNull Context context, int resource, List<Student>objects) {
super(context, resource, objects);
resourceId=resource;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Student student=getItem(position);
// View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolder=new ViewHolder();
viewHolder.studentName=(TextView)view.findViewById(R.id.student_name);
viewHolder.studentAge=(TextView)view.findViewById(R.id.student_age);
viewHolder.StudentClass_name=(TextView)view.findViewById(R.id.student_class_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}
viewHolder.studentName.setText(student.getName());
viewHolder.studentAge.setText(String.valueOf(student.getAge()));
viewHolder.StudentClass_name.setText(student.getClass_name());
return view;
}
class ViewHolder{
TextView studentName;
TextView studentAge;
TextView StudentClass_name;
}
}
ListView显示界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/student_item">
</include>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
主活动:
public class MainActivity extends AppCompatActivity {
private List<Student>studentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initStudent();
StudentAdapter adapter=new StudentAdapter(MainActivity.this,R.layout.student_item,studentList);
ListView listView=(ListView)findViewById(R.id.lv);
listView.setAdapter(adapter);
}
private void initStudent(){
for(int i=0;i<6;i++){
Student s1=new Student("zzq",18,"三年一班");
studentList.add(s1);
Student s2=new Student("q",17,"三年一班");
studentList.add(s2);
Student s3=new Student("zq",19,"三年一班");
studentList.add(s3);
}
}
}
运行界面: