The AdapterView is a ViewGroup subclass whose child Views are determined by an Adapter that
binds to data of some type. AdapterView is useful whenever you need to display stored data (as opposed to resource strings or drawables) in your layout.
어댑터뷰AdapterView는 뷰그룹ViewGroup의 서브클래스이다. 그것의 자식child 뷰들은 몇 가지 타입의 데이터를 바인드bind하는 어댑터Adapter에 의해 결정된다. 어댑터뷰는 여러분의 레이아웃안에 저장된 데이터(리소스 문자열들이나 드로어블(dwables)에 반대되는 개념)를 표시하고자 할 때 유용하다.
Gallery, ListView, and Spinner are examples of AdapterView subclasses that you can use to bind to a specific type of data and display it in a certain way.
갤러리Gallery, 리스트뷰ListView, 그리고 스피너Spinner는 여러분이 특정 타입의 데이터를 바인드해서 그것을 어떤 방식으로 표시하기 위해 사용할 수 있는 어댑터뷰AdapterView의 서브클래스의 예제이다.
AdapterView objects have two main responsibilities:
어댑터뷰AdapterView 오브젝트는 두 가지의 주요 책무를 가고 있다.
Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retireves data from an external source (perhaps a list that
the code supplies or query results from the device's database).
레이아웃에 데이터를 삽입하는 것은 일반적으로 어댑터뷰AdapterView 클래스를 어댑터Adapter에 바인딩하는 것에 의해 일반적으로 이루어진다. 어댑터는 코드에서 제공하는 리스트 또는 디바이스의 데이터베이스에 대한 쿼리 결과와 같은 외부 소스로부터 데이터를 가져온다.
The following code sample does the following:
다음에 나오는 코드 샘플은 다음과 같은 것을 수행한다.
Spinner with an existing View and binds it to a new ArrayAdapter
that reads an array of colors from the local resources.Contacts.People).
// 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 adatper
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);
Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.
커서어댑터CursorAdapter를 함께 사용되는 프로젝션projection에서 People._ID 컬럼을 가지는 것이 필요하다는 것에 주목하라. 그렇지 않으면, 여러분은 예외exception을 얻을 것이다.
If, during the course of your application's life, you change the underlying data that is read by your Adapter,
you should call notifyDataSetChanged(). This will notify the attached View
that the data has been changed and it should refresh itself.
만약 여러분의 애플리케이션이 존속하는 동안 여러분의 어댑터Adapter에 의해 읽혀지게 되는 기본적인 데이터를 여러분이 바꾼다면, 여러분은 notifyDataSet Changed()를 호출해야 한다. 이것은 첨부된 어댑터뷰AdapterView에게 데이터가 변경되었고, 그래서 스스로를 갱신해야 한다는 것을 통보할 것이다.
You handle the user's selecction by setting the class's AdapterView.OnItemClickListener member to a listener and
catching the selection changes.
여러분은 그 클래스의 AdapterView.OnItemClickListener 멤버를 리스너listener로 설정하고 사용자가 선택한 것에 대한 변화를 감지catching함으로써, 사용자가 선택한 것을 처리한다.
// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Display a messagebox.
Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();
}
};
// Now hook into our object and set its onItemClickListener member
// to our class handler object.
mHistoryView = (ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);
For more discussion on how to create different AdapterViews, read the following tutorials: Hello Spinner, Hello ListView, and Hello GridView.
다른 형태의 어댑터뷰AdapterViews를 만드는 방법에 대한 더 많은 논의에 대해서, 뒤에 나오는 Hello Spinner, Hello ListView, 그리고 Hello GridView와 같은 튜토리얼을 읽어라.