SimpleAdapter 的用法
1. 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@android:color/holo_blue_dark" android:dividerHeight="2dip" /> </RelativeLayout>
2. simple adapter xml布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="horizontal" > 6 7 8 <ImageView 9 android:id="@+id/iv_2" 10 android:layout_width="100dp" 11 android:layout_height="100dp" 12 android:layout_marginLeft="10dp" 13 android:layout_alignParentLeft="true" 14 15 /> 16 <TextView 17 android:id="@+id/tv_2" 18 android:layout_width="wrap_content" 19 android:layout_height="100dp" 20 android:textSize="40dp" 21 android:layout_toRightOf="@+id/iv_2" 22 android:gravity="bottom" 23 24 /> 25 26 <Button 27 android:id="@+id/bt_1" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_alignParentRight="true" 31 android:layout_centerVertical="true" 32 android:textSize="20dp" /> 33 34 </RelativeLayout>
3. Java文件
1 package com.jtl.simple_adapter; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 7 import android.os.Bundle; 8 import android.app.Activity; 9 import android.app.Application; 10 import android.content.pm.ApplicationInfo; 11 import android.util.Log; 12 import android.view.Menu; 13 import android.widget.ListView; 14 import android.widget.SimpleAdapter; 15 16 public class MainActivity extends Activity { 17 18 /* 19 *定义ListView对象 20 *存放图片id数组 21 *定义简单简单适配器对象 22 *创建ListView对象 23 * **/ 24 ListView lv; 25 int[] imagId=new int[10]; 26 SimpleAdapter adapter; 27 List list; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 33 34 //快速获取图片id 35 /*getResources().getIdentifier 调用该方法用老获取图片ID 36 * "drawable" 图片文件路径(一般就是drawable) 37 * info.packageName 调用图片文件的包名 38 * **/ 39 ApplicationInfo info=getApplicationInfo(); 40 for(int i=0;i<imagId.length;i++){ 41 imagId[i]=getResources().getIdentifier("a"+(i+1), 42 "drawable",info.packageName);} 43 Log.e("huhu",imagId[1]+""); 44 /* 45 * list实例化 46 * HashMap map 创建HashMap对象,存放数据 47 * 一共有十组数据,因此需要创建10个HashMap对象 48 * 最后将map添加到list中去 49 * */ 50 list=new ArrayList(); 51 for(int i=0;i<imagId.length;i++){ 52 HashMap map=new HashMap(); 53 map.put("image", imagId[i]); 54 map.put("name", "张"+(1+i)); 55 map.put("confirm","确定"); 56 list.add(map); 57 } 58 /* 59 * @PARAM list 数据源 60 * R.layout.layout_1 布局文件id 61 * new String[]{"image","name"} string 数组对象 62 * new int[]{R.id.iv_2,R.id.tv_2} 存放布局id 63 * 64 * **/ 65 lv=(ListView) findViewById(R.id.lv_1); 66 adapter=new SimpleAdapter(MainActivity.this, 67 list, 68 R.layout.layout_1, 69 new String[]{"image","name","confirm"},new int[]{R.id.iv_2,R.id.tv_2,R.id.bt_1}); 70 //在ListView加入适配器 71 lv.setAdapter(adapter); 72 } 73 74 75 76 }