Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android ListView 入門(一):從版面配置到 SimpleAdapter

編輯:開發入門

ListView 是 Android 開發中出現頻率極高的控制項。它裡頭的每一個子項(Item)可以只是一段純文字,也可以是一個由多個元件組合起來的複合版面。

要做出 ListView,基本上分三個步驟:準備要顯示的資料、設計 Item 的版面、最後用 Adapter 把兩者串起來。我們一步一步來。

第一步:主程式的版面檔(main.xml)

先在 main.xml 裡放一個 ListView 作為 Item 的容器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/MyListView">
    </ListView>

</LinearLayout>

第二步:Item 的版面檔(my_listitem.xml)

接著設計單一 Item 長什麼樣子。這裡用一個垂直 LinearLayout 包兩個 TextView,一個放標題、一個放內容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:id="@+id/MyListItem"
    android:paddingBottom="3dip"
    android:paddingLeft="10dip">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/ItemTitle"
        android:textSize="30dip">
    </TextView>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/ItemText">
    </TextView>

</LinearLayout>

這裡用到的幾個屬性說明如下:

  • paddingBottom="3dip":Layout 底部留出 3 個像素的空白。
  • paddingLeft="10dip":Layout 左側留出 10 個像素的空白。
  • textSize="30dip":TextView 的字級為 30dip。

第三步:Java 程式碼把資料綁上去

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 綁定 XML 中的 ListView,作為 Item 的容器
    ListView list = (ListView) findViewById(R.id.MyListView);

    // 建立動態陣列並載入資料
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 30; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("ItemTitle", "This is Title.....");
        map.put("ItemText", "This is text.....");
        mylist.add(map);
    }

    // 建立 Adapter,把陣列對應成 ListItem
    SimpleAdapter mSchedule = new SimpleAdapter(
        this,                                              // Context
        mylist,                                            // 資料來源
        R.layout.my_listitem,                              // Item 的 XML 實作
        new String[] {"ItemTitle", "ItemText"},            // 動態陣列中要對應的 key
        new int[] {R.id.ItemTitle, R.id.ItemText});        // Item 版面裡兩個 TextView 的 ID

    // 綁定並顯示
    list.setAdapter(mSchedule);
}

這裡最關鍵的觀念是 SimpleAdapter 的運作方式:它接受一個 Map 清單作為資料來源,再透過兩個平行陣列完成對應——String[] 指定要取出哪些 key,int[] 指定要塞進哪些 View 的 ID。兩個陣列的順序必須一一對應,順序錯了資料就會顯示錯位。

另外提醒:真實專案中不建議在迴圈裡產生大量假資料,實務上會改用 BaseAdapter 並搭配 ViewHolder 模式來複用 Item View,避免大量 findViewById 造成效能低落。這部分留待下一篇再談。

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