接上文

         上一篇文章使用的是ListView中最简单的ArrayAdapter,而且可以很容易的看出,ArrayAdapter比较适合

单一的显示一些文本信息。不过这并不表明使用ArrayAdapter不能显示图片的信息。如下:

        适配器需要使用的xml文件list.xml,其中TextView必须指定id,适配器需要

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical" >      <LinearLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="horizontal" >          <ImageView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="@drawable/th_notesalt" />          <TextView             android:id="@+id/text1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textColor="#000000"             android:textSize="18sp" />     </LinearLayout>  </LinearLayout>
        其他部分和上一篇文章中并没有两样,只是在创建适配器的时候略有不同:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 				R.layout.list,R.id.text1 ,getList());//多出一个参数xml中的TextView的id
         显示的结果图:


           
       不过从这里可以看出,ArrayAdapter对一些复杂的视图确实不适用,可以考虑用SimplAdapter来实现。
 SimpleAdapter的扩展性最好,可以定义各种各样的布局,可以放上格式的UI组件。SimpleAdapter的主要
作用就是将List集合的数据转换为ListView可以使用的格式。而要实现这种转换,首先需要定义一个布局文件
作为数据的显示模板,其次,数据是以键值对的形式(Map)存放在List集合中的,这样布局文件中的UI组件
的ID实际应该和Map对象里面的“键”(Key)对应,至于组件的实际内容则由Map对象的Value决定。
      下面给出程序例子:(玩过dota,就以dota中的英雄为列表)
       hero_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="horizontal" >      <ImageView         android:id="@+id/img"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="5px"        />      <LinearLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="vertical" >          <TextView             android:id="@+id/title"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textColor="#FFFFFFFF"             android:textSize="10px"             android:layout_gravity="center"              android:gravity="center"/>          <TextView             android:id="@+id/info"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="7px"              android:textColor="#31B6EF"/>     </LinearLayout>  </LinearLayout>
        MainActivity.java中的代码:
package com.example.layout_listview_simpleadapter;  import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;  import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast;  public class MainActivity extends Activity {  	private ListView listView = null; 	protected void onCreate(Bundle savedInstanceState) { 		// TODO Auto-generated method stub 		super.onCreate(savedInstanceState); 		setContentView(R.layout.activity_main); 		listView = (ListView)findViewById(R.id.list); 		SimpleAdapter adapter = new SimpleAdapter(this, getData(), 				R.layout.hero_list, new String[] { "title", "info", "img" }, 				new int[] { R.id.title, R.id.info, R.id.img }); 		  listView.setAdapter(adapter); 	}  	private List<Map<String, Object>> getData() { 		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 		Map<String, Object> map = new HashMap<String, Object>(); 		map.put("title", "敌法师"); 		map.put("info", "  近卫敌法师,法师的噩梦,快速的***率,瞬间的blink让他在战场上来取自如!!!!"); 		map.put("img", R.drawable.difa); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "水晶室女"); 		map.put("info", "  经过寒冰泉巨魔冰冻魔法师Raishali的长年训练,Rylai善于运用令人叹为观止的禁制魔法,她的绝技是异常强大的范围杀伤技能。"); 		map.put("img", R.drawable.bingnv); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "痛苦女王"); 		map.put("info", "  Akasha在被巫妖王复活成女妖之后,她成功地征服了一个魔女,并占据了她的肉身。她最喜欢用魔女的毒击和女妖的嚎叫折磨敌人,是名副其实的“痛苦女王”"); 		map.put("img", R.drawable.nvwang); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "地精修补匠"); 		map.put("info", "  Boush受过最为高阶的哥布林技术的教育。那些机械系统可以帮助他在相当远的距离外杀伤对手!"); 		map.put("img", R.drawable.xll); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "剑圣"); 		map.put("info", "  他出剑似舞蹈般灵动轻盈,无论多坚固的护甲都已悄然破碎:他行动如诗歌般优雅从容,无论多强的敌人弹指间灰飞烟灭"); 		map.put("img", R.drawable.jiansheng); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "船长"); 		map.put("info", "  在近位的所有盟军中,只有一个人,在许多大陆上都有着不同但又令人生畏的名字。他的追随者们叫他COCO船长。"); 		map.put("img", R.drawable.chuanzhang); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "兽王"); 		map.put("info", "  作为野兽的朋友,Rexxar善于用他那令人不安的野性撕扯敌人,而且为了胜利,他甚至会将自己的斧头往敌人身上扔"); 		map.put("img", R.drawable.shouwang); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "半人马酋长"); 		map.put("info", "  即使以半人马的标准来衡量,他们的族长Bradwarden的身材仍然称得上是“远古巨兽”。"); 		map.put("img", R.drawable.qiuzhang); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "牛头人酋长"); 		map.put("info", "  撼地神牛,这个技艺娴熟的年轻牛头人,寻求着战斗,以创造新的传奇。"); 		map.put("img", R.drawable.niushao); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "全能骑士"); 		map.put("info", "  作为神的使者,Purist能够借助神圣力量使用各种支援与防御技能"); 		map.put("img", R.drawable.quanneng); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "熊猫酒仙"); 		map.put("info", "  他是最难以杀死的英雄之一,他的***迅捷而有力。他已经实在无法计算到底有多少人倒在了他的脚下。"); 		map.put("img", R.drawable.jiuxian); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "流浪剑客"); 		map.put("info", "  作为骑士和暗夜精灵的后代,Sven一出生就被放逐,从此在独处中冥想和训练自己。"); 		map.put("img", R.drawable.siwen); 		list.add(map); 		map = new HashMap<String, Object>(); 		map.put("title", "山岭巨人"); 		map.put("info", "  由Ashenra嶙峋的山脉孕育而生,Tiny可以通过投掷大量的泥石造成山崩!"); 		map.put("img", R.drawable.xiaoxiao); 		list.add(map); 		return list; 	}   	public boolean onCreateOptionsMenu(Menu menu) { 		// Inflate the menu; this adds items to the action bar if it is present. 		getMenuInflater().inflate(R.menu.main, menu); 		return true; 	}  } 
             附上效果图:

               
    上述代码中红色部分为关键部分:
           
                     SimpleAdapter adapter = new SimpleAdapter(this, getData(), 				R.layout.hero_list, new String[] { "title", "info", "img" }, 				new int[] { R.id.title, R.id.info, R.id.img });
       这里就是SimpleAdapter的构造,他指定了所有的数据类型为Map对象,即getData()返回的是一个Map对象
并且指定了显示数据的模板(R.ayout.hero_list),而且这个模板还进一步制定了ListView中数据显示的格式,
每一个map对象的Key都会与模板ID进行匹配,以其指定的格式显示。容易知道的是一个Map对象就是ListView
的一行。
     
           以下是完整源码:
                    http://download.csdn.net/detail/kiritor/5126322