MainActivity7
package com.example.myapplication_one;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity7 extends AppCompatActivity {
ListView listview1;
private String[] data = {"济南","淄博","威海"};
List<Student> list = new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//用于加载页面布局 R.layout.名称
setContentView(R.layout.layout6);
init();
initStudent();//初始化学生类
/*
* Adapter:桥梁的作用,连接控件与数据的桥梁
* ArrayAdapter:数组适配器
* context:上下文
* resource:每一个Item显示的样式
* objects:需要绑定的数据源
*/
//用于连接简单的数据源与控件
/* ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity7.this,
android.R.layout.simple_list_item_1,//简单的显示一个textview样式
data); */
MyAdapter myAdapter = new MyAdapter(MainActivity7.this,R.layout.layout7,list);
//控件绑定适配器
listview1.setAdapter(myAdapter);
}
//初始化学生数据
private void initStudent(){
for(int i=0; i<3 ;i++){
Student student1 = new Student(R.drawable.a1,"2019001");
list.add(student1);
Student student2 = new Student(R.drawable.a1,"2019002");
list.add(student2);
Student student3 = new Student(R.drawable.a1,"2019003");
list.add(student3);
}
}
private void init() {
listview1 = findViewById(R.id.list_1);
}
}
layout6
<?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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我爱你,中国🇨🇳" />
<ListView
android:id="@+id/list_1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
layout7
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100px"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_1"
android:layout_width="50px"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MyAdapter.java
package com.example.myapplication_one;
import android.content.Context;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends ArrayAdapter<Student> {
private int resourceId;
public MyAdapter(Context context, int resource , List<Student> list) {
super(context, resource,list);
resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Student student = getItem(position);
//java在这个地方不能通过findbyid()方法,使用inflate方法找到Item的页面布局
/*
* inflate(resourceId,parent,false);
* 第三个参数:表示只让在父类容器中声明的layout属性有效,
* 但不会为这个View添加父类布局
* 因为一旦view有了父类布局,就无法添加到listView中了。
*/
View view = LayoutInflater.from(getContext())//getContext()获取上下文
.inflate(resourceId,parent,false);//parent:父类容器,在这里当作根结点
ImageView imageView =
(ImageView) view.findViewById(R.id.img_1);
TextView textView =
(TextView) view.findViewById(R.id.textView_1);
imageView.setImageResource(student.getImgId());
textView.setText(student.getNum());
return view;
}
}
Student.java
package com.example.myapplication_one;
public class Student {
private int imgId;
private String num;
public Student(int imgId,String num){
this.imgId = imgId;
this.num = num;
};
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/76af2ec6d8092b066f505f38c502b212.png)