Android笔记 simpleAdapter demo

来源:互联网 发布:伏尔加河上的纤夫知乎 编辑:程序博客网 时间:2024/05/28 23:19

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 {@Overrideprotected 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 [] 布局文件里面的idlv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item,new String[] { "nametext", "iconid" }, new int[] { R.id.tv,R.id.iv }));}}


0 0
原创粉丝点击