Android笔记 simpleAdapter demo

1取任意五张图片(不要太大)放到drawable-hdpi文件夹下,我取的系统自带图标

sym_action_add.png
sym_action_call.png
sym_action_chat.png
sym_action_email.png
sym_call_incoming.png

2布局


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout>


list_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:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

3MainActivity

package com.example.a39_simpleadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ListView lv = (ListView) findViewById(R.id.lv);
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("nametext", "我是功能一");
		map1.put("iconid", R.drawable.sym_action_add);

		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("nametext", "我是功能二");
		map2.put("iconid", R.drawable.sym_action_call);

		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("nametext", "我是功能三");
		map3.put("iconid", R.drawable.sym_action_chat);

		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("nametext", "我是功能四");
		map4.put("iconid", R.drawable.sym_action_email);

		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("nametext", "我是功能五");
		map5.put("iconid", R.drawable.sym_call_incoming);

		data.add(map1);
		data.add(map2);
		data.add(map3);
		data.add(map4);
		data.add(map5);

//		data 绑定的数据.list集合
//		R.layout.list_item,数据显示对应的布局
//		要让数据和view对象建立一个映射关系
//		from [] map集合里面数据的key
//		to [] 布局文件里面的id
		
		lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item,
				new String[] { "nametext", "iconid" }, new int[] { R.id.tv,
						R.id.iv }));
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值