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

Android 簡易實戰教程(十八):ListView 顯示與 SimpleAdapter 入門

編輯:關於 Android 程式設計

ListView 的適配器有好幾種:ArrayAdapter、BaseAdapter、SimpleAdapter 等等。本篇先熱身,用最簡單的 SimpleAdapter 把資料呈現出來。

一、主版面

先在 activity_main.xml 放一個 ListView:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

二、單項版面

再為每個列表項準備一個版面檔 item_listview.xml。這裡放一張圖片加一段文字:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/photo3" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="22sp" />
</LinearLayout>

三、在 Activity 中綁定資料

SimpleAdapter 要求資料以「List<Map>」的形式提供:每個 Map 代表一列,key 對應版面中的控制項,value 是實際要顯示的內容。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView) findViewById(R.id.lv);

        // 每個 Map 對應一個列表項,封裝該項需要的所有資料
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("photo", R.drawable.photo1);
        map1.put("name", "小志的兒子");
        data.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("photo", R.drawable.photo2);
        map2.put("name", "小志");
        data.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("photo", R.drawable.photo3);
        map3.put("name", "趙帥哥");
        data.add(map3);

        lv.setAdapter(new SimpleAdapter(
                this,
                data,
                R.layout.item_listview,
                new String[]{"photo", "name"},          // from:Map 的 key
                new int[]{R.id.iv_photo, R.id.tv_name}  // to:目標控制項 id
        ));
    }
}

四、參數別搞反

SimpleAdapter 的建構子參數依序是:Context、資料來源、item 版面 id、from、to。這裡最容易出錯的就是 from 與 to:

  • from 是字串陣列,內容必須與 Map 的 key 完全一致。
  • to 是 int 陣列,代表資料要填進哪個控制項。
  • 兩者的順序必須一一對應,photo 對 iv_photo、name 對 tv_name。順序錯了不一定編譯失敗,但執行時就會出現圖文錯位甚至報錯。

五、執行結果

六、SimpleAdapter 的極限

SimpleAdapter 適合版面固定、資料單純的場景。但一旦需要「依資料內容動態改變樣式」、「為不同型別的項目使用不同版面」,或「只更新其中一項而不重繪全部」,它就顯得力不從心——那時就該換成繼承 BaseAdapter 自訂 Adapter,並搭配 ViewHolder 模式來複用 convertView。

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