AdapterView
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تجربة طريقة ComposeAllowed
Jetpack Compose هي مجموعة أدوات واجهة المستخدم التي ننصح بها لنظام التشغيل Android. تعرَّف على كيفية استخدام التنسيقات في Compose.
AdapterView
هو
ViewGroup
تعرض العناصر التي تم تحميلها في محوِّل. تشير رسالة الأشكال البيانية
يأتي النوع الأكثر شيوعًا من المحوّلات من مصدر بيانات مستنِد إلى مصفوفة.
يوضّح هذا الدليل كيفية إكمال العديد من الخطوات الرئيسية ذات الصلة بإعداد
محوّل.
ملء التخطيط بالبيانات
لإضافة بيانات إلى التنسيق الذي تنشئه في واجهة المستخدم لتطبيقك، أضِف رمزًا
مشابه لما يلي:
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()
. سيؤدي ذلك إلى إرسال إشعار إلى العرض المرفق.
بأن البيانات قد تم تغييرها وينبغي تحديثها تلقائيًا.
ملاحظة: في الإصدار 3.6 من "استوديو Android" والإصدارات الأحدث،
يمكن أن تحل ميزة ربط العرض محل
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);
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# AdapterView\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to work with layouts in Compose. \n[Lists and Grids →](/jetpack/compose/lists#lazy) \n\n`AdapterView` is a [`ViewGroup`](/reference/android/view/ViewGroup) that displays items loaded into an adapter. The\nmost common type of adapter comes from an array-based data source.\n\nThis guide shows how to complete several key steps related to setting up\nan adapter.\n\nFill the layout with data\n-------------------------\n\nTo add data into the layout that you create in your app's UI, add code\nsimilar to the following: \n\n### Kotlin\n\n```kotlin\nval PROJECTION = arrayOf(Contacts.People._ID, People.NAME)\n...\n\n// Get a Spinner and bind it to an ArrayAdapter that\n// references a String array.\nval spinner1: Spinner = findViewById(R.id.spinner1)\nval adapter1 = ArrayAdapter.createFromResource(\n this, R.array.colors, android.R.layout.simple_spinner_item)\nadapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)\nspinner1.adapter = adapter1\n\n// Load a Spinner and bind it to a data query.\nval spinner2: Spinner = findViewById(R.id.spinner2)\nval cursor: Cursor = managedQuery(People.CONTENT_URI, PROJECTION, null, null, null)\nval adapter2 = SimpleCursorAdapter(this,\n // Use a template that displays a text view\n android.R.layout.simple_spinner_item,\n // Give the cursor to the list adapter\n cursor,\n // Map the NAME column in the people database to...\n arrayOf(People.NAME),\n // The \"text1\" view defined in the XML template\n intArrayOf(android.R.id.text1))\nadapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)\nspinner2.adapter = adapter2\n```\n\n### Java\n\n```java\n// Get a Spinner and bind it to an ArrayAdapter that\n// references a String array.\nSpinner s1 = (Spinner) findViewById(R.id.spinner1);\nArrayAdapter adapter = ArrayAdapter.createFromResource(\n this, R.array.colors, android.R.layout.simple_spinner_item);\nadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);\ns1.setAdapter(adapter);\n\n// Load a Spinner and bind it to a data query.\nprivate static String[] PROJECTION = new String[] {\n People._ID, People.NAME\n };\n\nSpinner s2 = (Spinner) findViewById(R.id.spinner2);\nCursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);\n\nSimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,\n android.R.layout.simple_spinner_item, // Use a template\n // that displays a\n // text view\n cur, // Give the cursor to the list adapter\n new String[] {People.NAME}, // Map the NAME column in the\n // people database to...\n new int[] {android.R.id.text1}); // The \"text1\" view defined in\n // the XML template\n\nadapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);\ns2.setAdapter(adapter2);\n```\n\nNote that it is necessary to have the People._ID column in projection used with CursorAdapter\nor else you will get an exception.\n\nIf, during the course of your application's life, you change the underlying data that is read by your Adapter,\nyou should call [notifyDataSetChanged()](/reference/android/widget/ArrayAdapter#notifyDataSetChanged()). This will notify the attached View\nthat the data has been changed and it should refresh itself.\n\n**Note:** With Android Studio 3.6 and higher, the\n[view binding](/topic/libraries/view-binding) feature can replace\n`findViewById()` calls and provides compile-time type safety for\ncode that interacts with views. Consider using view binding instead of\n`findViewById()`.\n\nHandle user selections\n----------------------\n\nYou handle the user's selection by setting the class's [AdapterView.OnItemClickListener](/reference/android/widget/AdapterView.OnItemClickListener) member to a listener and\ncatching the selection changes. \n\n### Kotlin\n\n```kotlin\nval historyView: ListView = findViewById(R.id.history)\nhistoryView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -\u003e\n Toast.makeText(context, \"You've got an event\", Toast.LENGTH_SHORT).show()\n}\n```\n\n### Java\n\n```java\n// Create a message handling object as an anonymous class.\nprivate OnItemClickListener messageClickedHandler = new OnItemClickListener() {\n public void onItemClick(AdapterView parent, View v, int position, long id)\n {\n // Display a messagebox.\n Toast.makeText(context,\"You've got an event\",Toast.LENGTH_SHORT).show();\n }\n};\n\n// Now hook into our object and set its onItemClickListener member\n// to our class handler object.\nhistoryView = (ListView)findViewById(R.id.history);\nhistoryView.setOnItemClickListener(messageClickedHandler);\n``` \nFor more discussion see the [Spinner](/guide/topics/ui/controls/spinner) topic."]]