读书人

ListView里面怎么获取Spinner对象

发布时间: 2012-12-27 10:17:09 作者: rapoo

ListView里面如何获取Spinner对象.
大侠们好,
现在我的Activity中的用的是这个布局,里面有个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>

我的Activity用的是最上面listview_layout.xml布局,
我的Activity中 用SimpleAdapter引用了listitem_layout.xml布局

问题:

我现在想在ListView的Activity里面获取到listview_layout.xm布局中的Spinner控件ID
怎么获取?
直接在ListView的 Activity中 findViewById(MySpinner) 会报空指针异常,找不到.
因为Activity中的布局文件用的是listview_layout.xml的,
而Spinner控件在这个listitem_layout.xml这个里面.

我是新手 大虾帮忙啊 赶紧不尽

[最优解释]
你不能直接使用 findViewById(spinnerId)。你应该重写Custom Adapter中的getView()方法,而不是Simple Adapter里面的方法。在这个方法里inflate listitem_layout到一个view(v),然后使用 v.findViewById(spinnerId).
看这个链接,可以帮助你创建custom adapter。例子中ImageView对象是在GetView方法中创建的。
[其他解释]
写个内部类的adapter里面不就可以view.findViewById()了


这样行不?同是新手的回答
[其他解释]
要在adapter中getview方法中写,
[其他解释]
要用自定义ListView
[其他解释]

引用:
写个内部类的adapter里面不就可以view.findViewById()了


这样行不?同是新手的回答


我看行
[其他解释]
谢谢大家
是的.
要在自定义的 Adapter的 getView里面写.

读书人网 >Android

热点推荐