2012-12-03 43 views
1

我的Activity的布局中有一个ListView。 listview_layout.xml如何在ListView中获得微调器

<?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列表项。

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的微调控件ID。

我在ListView的活动中使用findViewById(MySpinner),它有空指针异常的错误。 找不到。因为活动布局文件使用listview_layout.xml中的listview_layout.xml和Spinner控件。

任何人的帮助都非常感谢。

回答

3

你不能直接使用findViewById(spinnerId)。您必须重写自定义适配器的getView()方法而不是简单适配器,并在其中将listitem_layout充气到视图(v),然后使用v.findViewById(spinnerId)。

请参阅下面的link这将帮助您创建自定义适配器(在链接的示例中的天气适配器)。在该示例中,ImageView对象是在GetView方法中创建的。而不是你使用微调。

相关问题