2014-08-28 60 views
0

我想从包含多个视图的ListView项目中获取一个指定的视图。现在我有两个视图在我的xml布局文件中。Android ListView:从getChildAt()返回多个视图中的一个?

playlist_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center" > 
<Button 
    android:id="@+id/playlist_button" 
    android:layout_width="240dp" 
    android:layout_height="80dp" 
    android:background="@android:drawable/btn_default" 
    android:clickable="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:gravity="left|center_vertical|center_horizontal" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:textSize="21sp" /> 
</LinearLayout> 

而且我相关的XML布局到ListView:

ArrayAdapter<String> playlistAdapter = new ArrayAdapter<String>(context, R.layout.playlist_item, R.id.playlist_button, playlistNames); 
     listView.setAdapter(playlistAdapter); 

不过,我想获得一个给定的ListView项内的按钮。

此前我已经能够通过从铸造getChildAt()获得一个按钮,像这样的视图来实现:

Button b = (Button) listView.getChildAt(childIndex); 

可惜我不能再直接将其转换为一个按钮,因为现在它返回按钮和布局视图(?)我怎样才能得到按钮?谢谢!

+0

尝试视图V = listView.getChildAt(childIndex); 按钮b =(按钮)v.findViewById(R.id.playlist_button); – 2014-08-28 05:27:40

回答

2

使用

Button b = (Button) listView.getChildAt(childIndex).findViewById(R.id.playlist_button); 
+0

正是我想要的,谢谢! – pwang347 2014-08-28 05:40:48

相关问题