2010-08-08 4 views
2

내 활동이 ListActivity를 확장하지 않는다는 점에 유의하십시오. 그냥 활동. 다음과 같이Android의 여러 아이콘 ListView

나는 사용자 정의 레이아웃 목록보기가 있습니다

(고해상도/레이아웃/list_item.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 
<ImageView 
android:id="@+id/icon" 
android:layout_width="30dip" 
android:paddingTop="15dip" 
android:layout_height="wrap_content" 
android:src="@drawable/icon_default" 
/> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:textColor="#ffffff" 
/> 
    </LinearLayout> 

그때 사용 ArrayAdapter와에 ListView에 대한 어댑터를하고 있어요 여기서 'machines'라고하는 ArrayList.

ListView rooms = (ListView)findViewById(R.id.machines); 
ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, android.R.id.text1, machines); 
rooms.setAdapter(aa); 

ImageView 필드에서 비슷한 작업을 수행하려면 어떻게해야합니까?

답변

3

ArrayAdapter을 사용하면 getView()을 무시하고 더 많은 작업을 직접 수행해야합니다. 행을 얻으려면 행을 직접 팽창 시키거나 수퍼 클래스에 체인을 연결할 수 있습니다 (g etLayoutInflater().inflate()). 그런 다음 행에 ImageView을 찾아 제공된 위치 매개 변수를 기반으로 이미지를 getView()으로 설정합니다. 결과 행을 리턴하십시오.

Here's a free excerpt 많은 내용을 다루는 저서 중 하나입니다.

+1

굉장합니다. 그것은 완벽하게 작동했습니다. – jhirsch

0

또는 기본 어댑터를 사용하여 사용자 지정 목록보기를 만들 수 있습니다.