安卓自学笔记:7:用SimpleAdapter创建ListView

    不难理解的一段代码,因为自己粗心在for循环里少写了一句把item添加进去导致运行出来没有效果,好在还是弄出来了,能安心的睡觉了,明儿礼拜五,坚持一天,可以小小休息一下了,这天天上班还好,坐车坐的好累。。

代码如下:

public class MainActivity extends Activity {
	private String[] name1 = { "张三", "李四", "王五", "赵六" };
	private String[] name2 = { "热情的人", "闷骚的人", "活泼的人", "开朗的人" };
	private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
			R.drawable.p4 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ListView list = (ListView) findViewById(R.id.mylist);
		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < name1.length; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("img", images[i]);
			item.put("n1", name1[i]);
			item.put("n2", name2[i]);
			items.add(item);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, items,
				R.layout.simple_item, new String[] { "n1", "n2", "img" },
				new int[] { R.id.name1, R.id.name2, R.id.header });

		list.setAdapter(simpleAdapter);
	}

}


main的XML代码:

<?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="wrap_content"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

simple_item的XML代码:

<?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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textColor="#ff00"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/name2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

效果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值