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

Android SimpleAdapter 使用教學:快速綁定列表資料

編輯:關於 Android 程式設計

SimpleAdapter 是什麼

SimpleAdapter 適合將靜態或簡單結構的資料快速綁定到清單畫面。它接受由 Map 組成的 List,並依指定的 from 與 to 對應,把資料填入 XML 佈局中的元件。

建構參數解析

第一個參數是 Context;第二個是資料來源;第三個是單行佈局;第四個 from 指定 Map 的鍵;第五個 to 指定要填入的 View 資源。若 Map 缺少某個鍵,通常只會得到空值,不會讓程式崩潰,但畫面可能空白。

常見用法

例如要顯示頭像、名稱與描述,可準備包含 head、name、desc 三個鍵的 Map 清單,再讓 SimpleAdapter 對應到 ImageView 與兩個 TextView。若需要更複雜的互動,可自訂 ViewBinder 或改用 BaseAdapter。

SimpleAdapter adapter = new SimpleAdapter(this, listems, R.layout.simple_item, new String[] { "name", "head", "desc" }, new int[] { R.id.name, R.id.head, R.id.desc });
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved