1

アクティビティのレイアウトにListViewがあります。listview_layout.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="fill_parent" >

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

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のスピナーコントロールIDを取得したいと思います。

ListViewのアクティビティでfindViewById(MySpinner)を使用していますが、nullポインター例外のエラーがあります。見つけることができません。アクティビティレイアウトファイルは、listview_layout.xmlのlistview_layout.xmlとSpinnerコントロールを使用するためです。

誰かの助けはとてもありがたいです。

4

1 に答える 1

3

findViewById(spinnerId)を直接使用することはできません。SimpleAdapterの代わりにCustomAdapterのgetView()メソッドをオーバーライドし、その中でlistitem_layoutをview(v)に膨らませてから、v.findViewById(spinnerId)を使用する必要があります。

カスタムアダプタ(リンクの例ではWeather Adapter)の作成に役立つ以下のリンクを参照してください。この例では、ImageViewオブジェクトはGetViewメソッドで作成されます。その代わりに、Spinnerを使用します。

于 2012-12-03T08:08:41.947 に答える