1、 在activity布局文件中创建一个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" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
2、 创建一个adapter使用布局,里面放一个imageview和一个textview。
<?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="70dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="内容"
android:textSize="20sp" />
</LinearLayout>
3、 声明并添加数据源
// 数据源
private String[] name = { "北京北京", "济南济南", "成都", "好汉歌", "小手拉大手", "暖暖", "说谎", "男儿当自强", "今天" };
private int[] img = { R.drawable.heye, R.drawable.heye, R.drawable.heye, R.drawable.heye, R.drawable.heye,
R.drawable.heye, R.drawable.heye, R.drawable.heye, R.drawable.heye };
lists = new ArrayList<Map<String, Object>>();
for (int i = 0; i < name.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name[i]);
map.put("img", img[i]);
lists.add(map);
}
4、 初始化适配器
// 第一个参数是上下文,第二个参数是数据集,第三个参数是布局文件,第四个参数数据集中的map中的key,第四个参数是布局文件中的控件id
adapter = new SimpleAdapter(SimpleAdapterActivity.this, lists, R.layout.adapter_simple,
new String[] { "name", "img" }, new int[] { R.id.tv, R.id.img });
5、 绑定适配器
listView.setAdapter(adapter);
代码已上传百度云:
链接:https://pan.baidu.com/s/1fM7Ze9lYM4HrPIkpJMQUjg
提取码:7sx5
加入我的行列:
(嘿嘿嘿,点击这里关于我哦,分享小知识):https://www.jianshu.com/p/7d19f0df5b6b