【问题标题】:Android ListView: Return one of multiple views from getChildAt()?Android ListView:从getChildAt()返回多个视图之一?
【发布时间】:2014-08-28 05:25:09
【问题描述】:

我正在尝试从包含多个视图的 ListView 项中获取一个指定的视图。现在我的 xml 布局文件中有两个视图。

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 项中获取 Button。

以前我可以通过将从 getChildAt() 获得的 View 转换为 Button 来实现这一点,如下所示:

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

不幸的是,我不能再直接将其转换为按钮,因为现在它返回按钮和布局视图(?)我怎么能只得到按钮?谢谢!

【问题讨论】:

  • 尝试查看 v = listView.getChildAt(childIndex);按钮 b = (Button) v.findViewById(R.id.playlist_button);

标签: java android xml listview android-listview


【解决方案1】:

使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多