Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Adapter 使用教學(中):用 SimpleAdapter 打造圖文並茂的 ListView

編輯:關於 Android 程式設計

前面幾篇談的是 ArrayAdapter。用它在 ListView 裡擺純文字確實夠用,但實 projects 裡我們往往還想放圖片——這就是 SimpleAdapter 登場的時候了。

可能有朋友會問:第二篇不是已經能顯示圖片了嗎?沒錯,但那張圖是寫死在 XML 裡的,永遠只有那麼一張。根本原因在於我們傳給它的資料源只有字串,而且 ArrayAdapter 內部用的是 TextView,本質上也只吃文字。想做得更豐富,就得換個思路。

SimpleAdapter 其實一點也不 simple

使用 SimpleAdapter 的第一件事,是自己定義每一個 list item 的版面配置:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:padding="5dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80dip"
        android:layout_height="60dip"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="testing"/>

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"/>

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:layout_toRightOf="@+id/imageView1"
        android:layout_alignBottom="@+id/imageView1"/>
</RelativeLayout>

這裡放了 1 個 ImageView 與 2 個 TextView。

MainActivity 的實作

List<Map<String, Object>> simpleList = new ArrayList<Map<String, Object>>();

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

    ListView listView = (ListView) findViewById(R.id.listView1);
    SimpleAdapter adapter = new SimpleAdapter(this, simpleList, R.layout.simpleadapter,
            new String[] {"title", "content", "drawable"},
            new int[] {R.id.tvTitle, R.id.tvContent, R.id.imageView1});
    listView.setAdapter(adapter);
}

private void initSimpleList() {
    String[] titles = {"title1", "title2", "title3", "title4", "title5", "title6"};
    String[] contents = {"content1", "content2", "content3", "content4", "content5", "content6"};
    int[] drawableIds = {R.drawable.computer, R.drawable.excel, R.drawable.game,
            R.drawable.gc, R.drawable.network, R.drawable.pdf};
    Map<String, Object> map;
    for (int i = 0; i < 6; i++) {
        map = new HashMap<String, Object>();
        map.put("title", titles[i]);
        map.put("content", contents[i]);
        map.put("drawable", drawableIds[i]);
        simpleList.add(map);
    }
}

看懂 SimpleAdapter 的建構子

/**
 * @param context  執行該 SimpleAdapter 相關 View 的上下文
 * @param data     一個 Map 的 List,List 中每個項目對應列表中的一列,
 *                 Map 內需包含 "from" 指定的所有鍵
 * @param resource 定義 list item 各子 View 的版面配置資源 ID,
 *                 該配置至少要包含 "to" 中指定的那些 View
 * @param from     會被加入每一列 Map 的欄位名稱清單
 * @param to       用來顯示 "from" 對應值的 View,這些通常都是 TextView
 */
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
                     int resource, String[] from, int[] to) {
    mData = data;
    mResource = mDropDownResource = resource;
    mFrom = from;
    mTo = to;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

五個參數的角色其實很清楚:

  1. context:上下文環境。
  2. data:一個裝著 Map 的 List,每個 Map 就是一個 item 的資料。
  3. resource:這個 item 要用的版面配置,也就是我們自訂的那個。
  4. from 與 to:兩者搭配形成映射關係。from 指定 Map 的鍵,to 指定版面配置中對應的 View id。以上面的例子來說,Map 裡的 title 會被塞進 item 中的 tvTitle,其餘的綁定細節 SimpleAdapter 都替我們處理好了。

成果如下(樣式簡單,敬請見諒):

SimpleAdapter 名字叫 simple,用起來卻需要多寫一個版面配置檔。但只要理解了 from/to 這層映射關係,實際操作起來還是很直覺的。

有興趣的朋友也可以回頭看看前面關於 ArrayAdapter 的兩篇整理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved