6. 아답터 - 아이콘 리스트 뿌리기
Posted on March 12th, 2009 by christopherJ
아답터만 냅다 팠는데 벌써 다섯번째 예제가 됩니다. 이번에는 android.R.drawable 밑에 정의되어있는 시스템 아이콘을 리스트하는 프로그램을 만들었습니다. 이 프로그램은 SimpleAdapter 를 사용하는데 내부적으로 android.R.layout.activity_list_item 라는 시스템에서 제공하는 레이아웃을 사용합니다. 시스템 레이아웃를 직접보고 싶으면 SDK 에 tools/lib/res/default/layout 밑에 있는 XML 파일들을 참조합니다. 개발자 Reference 는 http://developer.android.com/reference/android/R.layout.html 에 있습니다.
.
android.R.layout.activity_list_item 의 모습은 다음과 같습니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="1dip"
android:paddingBottom="1dip"
android:paddingLeft="6dip"
android:paddingRight="6dip">
<ImageView android:id="@+id/icon"
android:layout_width="24dip"
android:layout_height="24dip"/>
<TextView android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="6dip" />
</LinearLayout>
이 내용을 보면 레이아웃을 잘 활용하는 것이 얼마나 안드로이드 프로그래밍을 쉽게 할수있습니다. 아이콘이미지의 리소스를 간단히 @+id/icon 에 매칭하여 ImageView 를 이용하여 아이콘 이미지를 출력합니다.
.
참고로 다른 두개의 가장 많이 사용하는 시스템 레이아웃을 보면
.
simple_list_item_1:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/listItemFirstLineStyle"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
.
simple_list_item_2:
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/listItemFirstLineStyle"/>
<TextView android:id="@android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
style="?android:attr/listItemSecondLineStyle" />
</TwoLineListItem>
.
이제 소스를 봅니다.
public class Adapter6 extends ListActivity {
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
HashMap<String,Integer> iconList = getConstants();
ArrayList<HashMap<String,Object>> mList = new ArrayList<HashMap<String,Object>>();
for(String iconName:iconList.keySet()) {
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("col1",iconName);
item.put("col2",iconList.get(iconName));
mList.add(item);
}
SimpleAdapter adapter6 = new SimpleAdapter(
this,
mList,
android.R.layout.activity_list_item,
new String[] { "col1","col2" },
new int[] { android.R.id.text1, android.R.id.icon } );
setListAdapter(adapter6);
}
private HashMap<String,Integer> getConstants() {
Class<?> c = android.R.drawable.class;
HashMap<String,Integer> iconList = new HashMap<String,Integer>();
Field[] flds = c.getFields();
for(Field f : flds) {
if(f.getType().equals(Integer.TYPE)) {
try {
iconList.put(f.getName(), f.getInt(null));
} catch(Exception e) { }
}
}
return iconList;
}
}
간단합니다.
(1) 아이콘을 참조할수있는 가장 좋은곳이 android.R.drawable 이므로 그곳에 가서 모든 int 를 읽었습니다
(2) 그 이름과 대응되는 Constant 넘버를 iconList 라는 HashMap 에 저장하고
(3) 이를 바탕으로 화면에 뿌릴때 사용될 ArrayList 를 만들었습니다.
(4) 이 ArrayList 를 SimpleAdapter 로 넘깁니다.