SimpleAdapter

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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值