AdapterView

روش نوشتن را امتحان کنید
Jetpack Compose ابزار رابط کاربری پیشنهادی برای اندروید است. یاد بگیرید که چگونه با طرح‌بندی‌ها در Compose کار کنید.

AdapterView یک ViewGroup است که آیتم‌های بارگذاری شده در یک آداپتور را نمایش می‌دهد. رایج‌ترین نوع آداپتور از یک منبع داده مبتنی بر آرایه می‌آید.

این راهنما نحوه انجام چندین مرحله کلیدی مربوط به راه‌اندازی آداپتور را نشان می‌دهد.

طرح را با داده پر کنید

برای افزودن داده‌ها به طرح‌بندی که در رابط کاربری برنامه خود ایجاد می‌کنید، کدی مشابه کد زیر اضافه کنید:

کاتلین

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

جاوا

// 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 در projection با CursorAdapter استفاده شود، در غیر این صورت با خطا مواجه خواهید شد.

اگر در طول عمر برنامه خود، داده‌های زیربنایی که توسط Adapter شما خوانده می‌شود را تغییر دهید، باید notifyDataSetChanged() را فراخوانی کنید. این کار به View متصل شده اطلاع می‌دهد که داده‌ها تغییر کرده‌اند و باید خود را به‌روزرسانی کند.

نکته: در اندروید استودیو ۳.۶ و بالاتر، ویژگی view binding می‌تواند جایگزین فراخوانی‌های findViewById() شود و امنیت نوع در زمان کامپایل را برای کدی که با viewها تعامل دارد، فراهم کند. استفاده از view binding را به جای findViewById() در نظر بگیرید.

مدیریت انتخاب‌های کاربران

شما با تنظیم عضو AdapterView.OnItemClickListener کلاس به یک شنونده و دریافت تغییرات انتخاب، انتخاب کاربر را مدیریت می‌کنید.

کاتلین

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()
}

جاوا

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

برای بحث بیشتر به تاپیک اسپینر مراجعه کنید.