AdapterView

試用 Compose
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中使用版面配置。

AdapterViewViewGroup,用於顯示載入至轉換器的項目。最常見的轉換器類型來自陣列式資料來源。

本指南說明如何完成幾個與設定轉換器相關的重要步驟。

在版面配置中填入資料

如要將資料新增至您在應用程式 UI 中建立的版面配置,請加入如下的應用程式程式碼:

Kotlin

val PROJECTION = arrayOf(Contacts.People._ID, People.NAME)
...

// Get a Spinner and bind it to an ArrayAdapter that
// references a String array.
val spinner1: Spinner = findViewById(R.id.spinner1)
val adapter1 = ArrayAdapter.createFromResource(
        this, R.array.colors, android.R.layout.simple_spinner_item)
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner1.adapter = adapter1

// Load a Spinner and bind it to a data query.
val spinner2: Spinner = findViewById(R.id.spinner2)
val cursor: Cursor = managedQuery(People.CONTENT_URI, PROJECTION, null, null, null)
val adapter2 = SimpleCursorAdapter(this,
        // Use a template that displays a text view
        android.R.layout.simple_spinner_item,
        // Give the cursor to the list adapter
        cursor,
        // Map the NAME column in the people database to...
        arrayOf(People.NAME),
        // The "text1" view defined in the XML template
        intArrayOf(android.R.id.text1))
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner2.adapter = adapter2

Java

// Get a Spinner and bind it to an ArrayAdapter that
// references a String array.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
    this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);

// Load a Spinner and bind it to a data query.
private static String[] PROJECTION = new String[] {
        People._ID, People.NAME
    };

Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);

SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
    android.R.layout.simple_spinner_item, // Use a template
                                          // that displays a
                                          // text view
    cur, // Give the cursor to the list adapter
    new String[] {People.NAME}, // Map the NAME column in the
                                         // people database to...
    new int[] {android.R.id.text1}); // The "text1" view defined in
                                     // the XML template

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter2);

請注意,投射的 People._ID 欄必須與 CursorAdapter 搭配使用,否則會發生例外狀況。

如要在應用程式的生命週期中,變更轉換器讀取的基礎資料,請呼叫 notifyDataSetChanged()。系統會通知附加的檢視,指出資料有所變更,且檢視應自行重新整理。

附註:在 Android Studio 3.6 以上版本中,檢視繫結功能可以取代 findViewById() 呼叫,並為與檢視互動的程式碼提供編譯時類型安全性。建議您使用檢視繫結,而非 findViewById()

處理使用者選項

如要處理使用者的選項,請將類別的 AdapterView.OnItemClickListener 成員設為事件監聽器並擷取選項異動。

Kotlin

val historyView: ListView = findViewById(R.id.history)
historyView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
    Toast.makeText(context, "You've got an event", Toast.LENGTH_SHORT).show()
}

Java

// Create a message handling object as an anonymous class.
private OnItemClickListener messageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        // Display a messagebox.
        Toast.makeText(context,"You've got an event",Toast.LENGTH_SHORT).show();
    }
};

// Now hook into our object and set its onItemClickListener member
// to our class handler object.
historyView = (ListView)findViewById(R.id.history);
historyView.setOnItemClickListener(messageClickedHandler);

如需進一步討論,請參閱輪轉選單主題。