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

SimpleAdapter 搭配 ListActivity 實作多欄清單

編輯:初階開發

這段時間都在看 Java,Android 放了一陣子,現在回過頭慢慢補。這次要做的是一個圖片加兩行文字的多欄清單。

要達到這個效果,最省事的做法是 ListActivity 搭配 SimpleAdapter。

資料結構:清單包 ArrayList,每列包 HashMap

每一列視為 ArrayList 的一個元素,而每一列內部的三個欄位則用 HashMap 存放:

ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.checked);
map.put("ItemTitle", "Level" + i);
map.put("ItemText", "this is under Level" + i);
list.add(map);

每一列中那三個元件(一個 ImageView、兩個 TextView)的排版,需要另外寫一個 XML 版面檔。

主程式

public class SimpleAdapterDemo extends ListActivity {

    // 填充 list 資料
    private ArrayList<HashMap<String, Object>> fillList() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; ++i) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.checked);
            map.put("ItemTitle", "Level" + i);
            map.put("ItemText", "this is under Level" + i);
            list.add(map);
        }
        return list;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<HashMap<String, Object>> list = fillList();

        // 建立适配器,把 ArrayList 的資料對應到畫面
        SimpleAdapter simAdapter = new SimpleAdapter(this, list,
                R.layout.items,                                        // 每一列的版面檔
                new String[]{"ItemImage", "ItemTitle", "ItemText"},    // 三個資料的索引鍵
                new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText}); // 對應的 View id
        setListAdapter(simAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        setTitle("點擊第" + position + "個項目");
    }
}

SimpleAdapter 的建構參數順序是:Context、資料清單、每列版面、資料鍵陣列、View id 陣列。兩組陣列的順序必須一一對應,這是最容易出錯的地方。

單列版面(items.xml)

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip">

    <ImageView
        android:id="@+id/ItemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="12dip"
        android:layout_alignParentRight="true" />

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

    <TextView
        android:id="@+id/ItemText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ItemTitle"
        android:text="TextView02" />
</RelativeLayout>

主版面(main.xml)

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

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <!-- 當 ListActivity 沒有資料時顯示這段 -->
    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="sorry, no data" />
</LinearLayout>

幾個實務提醒

使用 ListActivity 時,清單的 id 必須是 @id/android:list,空資料提示的 TextView 則必須是 @id/android:empty,兩者都是系統預設的約定,寫錯就會出錯。

另外,SimpleAdapter 適合結構固定的簡單清單。若每一列需要複雜的邏輯、非同步載圖或差異化排版,就該改用 BaseAdapter 自行實作 getView,並善用 ViewHolder 模式減少 findViewById 呼叫。

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