Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to get elements from listview header

I have this xml file

<?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>

and the listview is

<?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>

and the listview header is:

<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>

and on java activity i added the header to listview like this

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);

everything is good and the app is working , now I want to get the elements on the header of the listview , how? And how to set onclick listener for them?

like image 471
William Kinaan Avatar asked Dec 20 '25 01:12

William Kinaan


1 Answers

looks like you're already doing it.

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() .... );
like image 150
user123321 Avatar answered Dec 22 '25 16:12

user123321