Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
1 1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是: 2 ArrayAdapter可以让一个类继承自BaseAdapter之后,可以对listview中的button,checkBox等空间进行事件的监听操作,而SimpleAdapter只能对listview填充数据的一个操作,不具有对空间的事件监听功能。 3 下面通过实例进行说明(是通过自定义listview): 4 (1)SimpleAdapter: 5 listview中每一项中的数据的布局文件及时listItem.xml 6 <?xml version="1.0" encoding="utf-8"?> 7 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:gravity="center_horizontal" 11 android:orientation="horizontal" > 12 <ImageView 13 android:id="@+id/imageView1" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" /> 16 <LinearLayout 17 android:orientation="vertical" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" > 20 <TextView 21 android:focusable="false" 22 android:id="@+id/textView1" 23 android:textSize="14dp" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content"/> 26 27 <TextView 28 android:focusable="false" 29 android:id="@+id/textView2" 30 android:textSize="10dp" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" /> 33 34 </LinearLayout> 35 <ImageButton 36 android:focusable="true" 37 android:layout_gravity="center_vertical" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:id="@+id/imagebutton1"/> 41 </LinearLayout> 42 43 lisview的布局文件(list.xml): 44 45 <?xml version="1.0" encoding="utf-8"?> 46 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 47 android:layout_width="match_parent" 48 android:layout_height="match_parent" 49 android:orientation="vertical" > 50 <ListView 51 android:descendantFocusability="afterDescendants" 52 android:layout_width="fill_parent" 53 android:layout_height="wrap_content" 54 android:id="@+id/list" 55 android:scrollbars="vertical"></ListView> 56 </LinearLayout> 57 58 59 //此方法是从sdcard的RecorderFile文件夹中扫描到以.3gp结尾的文件存放到一个集合中 60 public List<Map<String,Object>> serachFile() 61 { 62 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 63 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 64 { 65 File file=new File(Environment.getExternalStorageDirectory().toString()+"/RecorderFile"); 66 File[] files=file.listFiles(); 67 68 for(int i=0;i<files.length;i++) 69 { 70 if(files[i].getName().endsWith(".3gp")) 71 { 72 //这个map对象必须在这里声明创建,否侧会出现数据重复显示一项数据 73 Map<String,Object> map=new HashMap<String,Object>(); 74 map.put("img", R.drawable.ic_launcher); 75 map.put("title", files[i].getName()); 76 map.put("info", files[i].getPath()); 77 map.put("button", R.drawable.control_play_blue); 78 list.add(map); 79 } 80 } 81 System.out.println("**************"+Environment.getExternalStorageState()); 82 } 83 return list; 84 } 85 86 下面是listview中最要的一部是数据填充器(SimpleAdapter) 87 SimpleAdapter simpleAdapter=new SimpleAdapter(this, serachFile(), 88 R.layout.listitem,new String[]{"img","title","info","button"}, 89 new int[]{R.id.imageView1,R.id.textView1,R.id.textView2,R.id.playButton}) 90 参数: 91 1.当前的类对象,可以通过thisl表示 92 2.这个参数是一个集合对象儿上面的方法正好是返回一个集合对象所以是serachFile() 93 3。这个是listview中每一项中的组件的布局文件 94 4.是map对象的以键值对以字符数组的形式 95 5.这个参数是,listview中每一项中的组件的id,把他以数组的方式存放 96 只有通过findviewById()找到listview控件并设置数据源。listview.setAdapter(simpleAdapter)这样就可以把数据填充到listview空间中