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

Android Studio 中的 ListView 入門實例:ArrayAdapter 快速上手

編輯:關於 Android 程式設計

做 Android 入門練習時,ListView 幾乎是必寫的第一個列表元件。最省事的做法是搭配 ArrayAdapter,幾行程式碼就能把一個字串陣列渲染成清單。

一、版面檔

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".ListViewArrayadapter">

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

</RelativeLayout>

這裡有個小細節值得注意:ListView 的 id 寫成 @android:id/list,這是系統內建的專用 id,只有當 Activity 繼承 ListActivity 時才需要這樣寫。

二、程式碼

package com.example.test.testlistview;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ListViewArrayadapter extends ListActivity {

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

        List<String> ls = new ArrayList<String>();
        ls.add("測試數據1");
        ls.add("測試數據2");
        ls.add("測試數據1");
        ls.add("測試數據2");
        ls.add("測試數據1");
        ls.add("測試數據2");
        ls.add("測試數據1");
        ls.add("測試數據2");
        ls.add("測試數據1");
        ls.add("測試數據2");
        ls.add("測試數據1");
        ls.add("測試數據2");

        // 指定系統內建樣式 simple_list_item_1,把資料灌進去
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, ls);
        setListAdapter(adapter); // 只有繼承 ListActivity 才有這個方法
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String s = ((TextView) v).getText().toString();
        Toast.makeText(this, "提示" + position + s, Toast.LENGTH_LONG).show();
        super.onListItemClick(l, v, position, id);
    }
}

三、關鍵點說明

  • ArrayAdapter 的兩個核心參數:一個版面樣式,一個資料集合。樣式決定每一長什麼樣子,資料集合決定顯示什麼。
  • android.R.layout.simple_list_item_1 是系統預建的單行文字樣式,直接拿来用就不必自己寫 XML。想自訂外觀,換成自己的 layout 即可。
  • setListAdapter() 是 ListActivity 提供的快捷方法;若 Activity 直接繼承 Activity,則改用 findViewById(R.id.list).setAdapter(adapter),此時版面中的 ListView id 也不必是系統 id。
  • onListItemClick() 同樣是 ListActivity 的便利回呼,省去自己設 OnItemClickListener 的麻煩,參數中的 position 就是點擊項目的索引。

這個例子麻雀雖小,卻把 ListView 的三要素講完了:資料來源、Adapter、列表元件。接下來只要把資料換成物件、把樣式換成自訂 layout 並自寫 Adapter,就能逐步長出真正的應用程式列表。

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