이번에는 지난 시간에 이어서 여러가지 ListView에 대해 알아보겠습니다.
1. 한 항목을 선택할 수 있는 ListView
마치 콤보박스와 같은 역할을 하는 ListView입니다.
2. 여러 항목을 선택할 수 있는 ListView
위에서 본 것과 다를 것이 별로 없습니다. 어댑터에서 리스트의 모양과 리스트에서 선택할 항목의 개수를 변경시켜주면 됩니다.
1. 한 항목을 선택할 수 있는 ListView
마치 콤보박스와 같은 역할을 하는 ListView입니다.
package com.androidhuman.ListExample;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListExample extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList list = new ArrayList();
list.add("커니의 안드로이드 이야기");
list.add("안드로이드사이드");
list.add("Google");
ArrayAdapter aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice, list);
ListView lv = getListView();
lv.setAdapter(aa);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
특별한 것은 없고, ArrayAdapter에서 레이아웃을 위와 같이 simple_list_item_single_choice로 바꿔주신 후, setChoiceMode()메소드를 통해 선택할 수 있는 항목의 개수를 설정해주시면 됩니다. 위의 경우는 한 가지만 선택하면 되므로, CHOICE_MODE_SINGLE을 사용하였습니다.
*XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:layout_height="wrap_content"
android:id="@+id/toAdd" android:hint="추가할 텍스트를 입력하세요."
android:layout_width="fill_parent"/>
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</LinearLayout>
2. 여러 항목을 선택할 수 있는 ListView
위에서 본 것과 다를 것이 별로 없습니다. 어댑터에서 리스트의 모양과 리스트에서 선택할 항목의 개수를 변경시켜주면 됩니다.
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, list); // multiple_item으로 변경
ListView lv = getListView();
lv.setAdapter(aa);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // CHOICE_MODE_MULTIPLE로 변경
강좌 작성환경
SDK Version : Android SDK 1.5, release 2
ADT Version : 0.9.1
ADT Version : 0.9.1
추후 SDK업데이트로 인해 글의 내용과 실제 내용간 차이가 있을 수 있습니다.
'유저 인터페이스 > 뷰(View)' 카테고리의 다른 글
| #11. List 집중공략! - (3) Custom ArrayAdapter를 이용한 ListView (50) | 2009.06.08 |
|---|---|
| 많은 양의 데이터 표시는 내게! - ListView (22) | 2009.06.04 |
| #08. TabView 및 FrameLayout을 이용한 탭 이용하기 (47) | 2009.04.08 |