自定义布局文件(listview的项的显示效果)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ImageView
android:id="@+id/image_photo"
android:layout_width=“70dp”
android:layout_height=“70dp”
android:padding=“10dp”/>
<TextView
android:id="@+id/name"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“20dp”
android:layout_toRightOf="@id/image_photo"/>
<TextView
android:id="@+id/age"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“20dp”
android:layout_toRightOf="@id/image_photo"
android:layout_below="@id/name"
android:layout_marginTop=“10dp”/>
student学生类
public class Student {
private String name;
private int age;
private int photo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView listView;
//自定义BaseAdapter
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);
L
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ist stuList=new ArrayList<>();
for(int i=0;i<10;i++){
Student stu=new Student();
stu.setAge(10+i);
stu.setName(“name”+i);
stu.setPhoto(R.mipmap.ic_launcher);
stuList.add(stu);
}
adapter=new MyAdapter(stuList,MainActivity.this);
listView.setAdapter(adapter);
}
MyAdapter
public class MyAdapter extends BaseAdapter {
private List stuList;
private LayoutInflater inflater;
public MyAdapter() {}
public MyAdapter(List stuList,Context context) {
this.stuList = stuList;
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return stuList==null?0:stuList.size();
}
@Override
public Student getItem(int position) {
return stuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//加载布局为一个视图
View view=inflater.inflate(R.layout.layout_student_item,null);
Student student=getItem(position);
//在view视图中查找id为image_photo的控件
ImageView image_photo= (ImageView) view.findViewById(R.id.image_photo);
TextView tv_name= (TextView) view.findViewById(R.id.name);
TextView tv_age= (TextView) view.findViewById(R.id.age);
image_photo.setImageResource(student.getPhoto());
tv_name.setText(student.getName());
tv_age.setText(String.valueOf(student.getAge()));