2012-12-03 6 views
1

내 활동의 레이아웃에 ListView가 있습니다. ListView에서 Spinner를 얻는 방법

<?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="fill_parent" > 

    <ListView android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/MyListView" /> 
</LinearLayout> 

listview_layout.xml 나는 ListView에의을 ListItem에 대한 또 다른 레이아웃을 사용하고 있습니다.

listitem_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  
    android:id="@+id/RelativeLayout01"  
    android:layout_width="fill_parent"  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" /> 

    <Spinner 
     android:id="@+id/MySpinner" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

내 활동의 레이아웃은 listview_layout.xml 년대와 동일합니다.

SimpleAdapter를 사용하여 listitem_layout.xml의 레이아웃을 인용했습니다.

난이도 : 이제 listview_layout.xm의 Spinner 컨트롤 ID를 얻고 싶습니다.

ListView의 활동에서 findViewById (MySpinner)를 사용하면 null 포인터 예외 오류가 발생합니다. 찾을 수 없습니다. Activity 레이아웃 파일은 listview_layout.xml과 Spinner 컨트롤을 listview_layout.xml에서 사용하기 때문입니다.

누군가의 도움은 매우 감사합니다.

답변

3

findViewById (spinnerId)를 직접 사용할 수 없습니다. Simple Adapter 대신 Custom Adapter의 getView() 메소드를 재정의해야하며, 내부에서 viewitem_layout을 뷰 (v)로 팽창시킨 다음 v.findViewById (spinnerId)를 사용해야합니다.

아래와 같이 link을 참조하십시오. 그러면 사용자 정의 어댑터 (링크 예제에서 Weather Adapter)를 만드는 데 도움이됩니다. 이 예제에서 ImageView 객체는 GetView 메서드로 만들어집니다. 대신 Spinner를 사용합니다.

관련 문제