AdapterView
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Compose 방식 사용해 보기
Jetpack Compose는 Android에 권장되는 UI 도구 키트입니다. Compose에서 레이아웃을 사용하는 방법을 알아보세요.
<ph type="x-smartling-placeholder">
</ph>
목록 및 그리드 →
를 통해 개인정보처리방침을 정의할 수 있습니다.
AdapterView
는 어댑터에 로드된 항목을 표시하는 ViewGroup
입니다. 가장 일반적인 어댑터 유형은 배열 기반 데이터 소스에서 가져옵니다.
이 가이드에서는 어댑터 설정과 관련된 몇 가지 주요 단계를 완료하는 방법을 설명합니다.
데이터로 레이아웃 채우기
앱 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
자바
// 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);
CursorAdapter와 함께 사용하는 투영에서 People._ID 열을 사용해야 합니다. 그렇지 않으면 예외가 발생합니다.
애플리케이션의 수명 동안 어댑터에서 읽은 기본 데이터를 변경하면 notifyDataSetChanged()
를 호출해야 합니다. 연결된 뷰에 데이터가 변경되었으며 새로고침 해야 함을 알립니다.
참고: Android 스튜디오 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()
}
자바
// 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);
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],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."]]