AdapterView

Thử cách sử dụng Compose
Jetpack Compose là bộ công cụ giao diện người dùng được đề xuất cho Android. Tìm hiểu cách sử dụng bố cục trong ứng dụng Compose.

AdapterView là một ViewGroup hiển thị các mục được tải vào trong một trình chuyển đổi (adapter). Loại trình chuyển đổi phổ biến nhất đến từ một nguồn dữ liệu dựa trên mảng (array).

Hướng dẫn này cho biết cách hoàn thành một số bước chính liên quan đến việc thiết lập trình chuyển đổi.

Điền dữ liệu vào bố cục

Để thêm dữ liệu vào bố cục mà bạn tạo trong giao diện người dùng của ứng dụng, hãy thêm mã tương tự như sau:

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);

Lưu ý rằng cần phải có cột People._ID trong phép chiếu được sử dụng với CursorAdapter, nếu không bạn sẽ nhận được một trường hợp ngoại lệ.

Nếu trong quá trình hoạt động của ứng dụng, bạn thay đổi dữ liệu cơ bản mà Adapter đọc ra, thì bạn nên gọi cho notifyDataSetChanged(). Việc này sẽ thông báo cho Thành phần hiển thị (View) đính kèm rằng dữ liệu đã được thay đổi và dữ liệu đó sẽ tự làm mới.

Lưu ý: với Android Studio 3.6 trở lên, tính năng liên kết thành phần hiển thị (view binding) có thể thay thế findViewById() và đem đến sự an toàn loại thời gian biên dịch cho mã tương tác với thành phần hiển thị. Hãy cân nhắc sử dụng chế độ liên kết thành phần hiển thị thay vì findViewById().

Xử lý các lựa chọn của người dùng

Bạn xử lý lựa chọn của người dùng bằng cách đặt thành viên AdapterView.OnItemClickListener của lớp thành một trình nghe (listener) và nắm bắt các thay đổi về lựa chọn.

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);

Để thảo luận thêm, hãy xem chủ đề Spinner.