activity_main.xml:
<RelativeLayout
android:width="match_parent"
android:height="60dp"
android:background="@android:color/holo_blue_bright">
<Button
android:id= "@+id/btnBack"
android:centerVertical="true"
android:text="返回"
android:width="wrap_content"
android:height="wrap_content"
android:visibility = "gont"/>
<TextView
android:width="wrap_content"
android:height="wrap_content"
android:text="学生列表"
android:textSize="20sp"
android:centerVertical="true"
android:centerHorizontal="true"/>
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/listViewStudent"
></ListView>
MainActivity.java:
private ListView listViewStudent;
this.listViewStudent = (ListView)this.findViewById(R.id.listViewStudent);
StudentAdapter studentAdapter = new StudentAdapter(this.getData(),this);
this.listViewStudent.setAdapter(studentAdapter);
private List<Student> getData(){
List<Student> data = new ArrayList<>();
for(int idx = 0;idx<30;idx++){
Student student = new Student();
student.setHeight(160 + idx);
student.setName("杨森");
if(idx % 2 ==0){
student.setSex("女");
}
else{
student.setSex("男");
}
data.add(student);
}
return data;
}
StudentAdapter.java extends BaseAdapter:
public studentAdapter(List<Student>,Activity activity){
this.studentList = studentList;
this.activity = activity;
}
private List<Student> studentList;
private Activity activity;
/*
显示Item的数量
*/
@Override
public int getCount() {
System.out.println("-----getCount-----");
return this.studentList.size();
}
@Override
public Object getItem(int position) {
System.out.println("-----getItem-----");
return this.studentList.get(position);
}
@Override
public long getItemId(int position) {
System.out.println("-----getItemId-----",+position);
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//System.out.println("-----getView----"+position);
Student student = this.studentList.get(position);
TextView tvName=(TextView)findViewById(R.id.tvName);
TextView tvHeight=(TextView)findViewById(R.id.tvHeight);
TextView tvSex=(TextView)findViewById(R.id.tvSex);
tvName.setName(student.getName());
tvheight.setName(student.getHeight());
tvSex.setName(student.getSex());
View view = View.inflate(this.activity,R.layout.listview_item_student,null);
return view;
}