安卓如何从ListView的头部获取元素

3
我有一个xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="20dip"
        android:gravity="center_horizontal"
        android:text="@string/app_name"
        android:textSize="25dip"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="150dip"
        android:layout_marginBottom="10dip"
        android:contentDescription="@string/iv_restaurantImage"
        android:src="@drawable/ic_launcher" />


    <ListView
        android:id="@+id/lv_restaurant_description_information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

列表视图是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dip"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:typeface="serif"
        android:textSize="15dip" />

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="@string/IV_LOGO_DESCRIPTION"
        android:textSize="20dip" />

</LinearLayout>

列表视图的标题是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rl_simple_list_item_header"

    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orderMeal"
        android:layout_marginLeft="20dip"
        android:id="@+id/tv_restaurant_description_orderMeal"
        android:drawableTop="@drawable/order_meal" />
    <TextView 
        android:id="@+id/iv_simple_list_item_header_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dip"
        android:drawableTop="@drawable/favorite"/>
</RelativeLayout>

在Java活动中,我像这样向ListView添加了标题。
lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
        View header = LayoutInflater.from(this).inflate(
                R.layout.simple_list_item_header, null);
        tv_favorite = (TextView) header
                .findViewById(R.id.iv_simple_list_item_header_favorite);
        lv_restaurantInformation.addHeaderView(header);

一切都很好,应用程序正在工作,现在我想获取列表视图标题栏上的元素,如何操作?并且如何为它们设置点击监听器?


1
我喜欢你的代码组织。 - user user
1个回答

2

看起来你已经在做了。

tv_favorite = (TextView) header
            .findViewById(R.id.iv_simple_list_item_header_favorite);

// now just add a click listener.
tv_favorite.setOnClickListener(new View.OnClickListener() .... );

这让我感到困惑,我按照你写的做了,但在单击文本框时没有得到任何结果。 - William Kinaan
如果点击没有反应,那么可能是有其他程序正在拦截。我不确定是什么导致了这种情况。 - user123321
是的,它可以工作,我只是重新启动了eClipse :),我会接受你的答案。 - William Kinaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接