Florian Mac Langlade
Florian Mac Langlade

Reputation: 1903

Android listview onItemClickListener doesn't work

Please excuse my English, I'm French !

I've got a listview, and I want to display an other activity on click on an item. But the onItemClickListener seems to doesn't work... I searched on Google, but but I found nothing.

Here my code:

The XML listView (custom) :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_advert_list_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background" >

<RelativeLayout
    android:id="@+id/advertBackground"
    android:layout_width="339dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="7.5dp"
    android:layout_marginTop="7.5dp"
    android:background="@drawable/background_advert_list" >

    <RelativeLayout
        android:id="@+id/advertImageLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp" >

        <RelativeLayout
            android:id="@+id/advertBackgroundLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/grey" >

            <ImageView
                android:id="@+id/advertImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/advertPriceLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/advertImage"
            android:layout_marginTop="-30.5dp"
            android:background="@color/white_50" >

            <TextView
                android:id="@+id/advertPriceText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:hint="@string/Price"
                android:paddingBottom="5dp"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:paddingTop="3dp"
                android:textSize="14dp"
                android:textStyle="bold" />
        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/advertInfosLayout"
        android:layout_width="305dp"
        android:layout_height="47dp"
        android:layout_below="@+id/advertImageLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" >

        <RelativeLayout
            android:id="@+id/advertInfosLayoutLayout"
            android:layout_width="225dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/advertImageLayout" >

            <TextView
                android:id="@+id/advertTitleText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/Product_name"
                android:textColor="@color/grey_blue"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/advertSizeText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/advertTitleText"
                android:hint="@string/Product_infos"
                android:textColor="@color/b4b5b6"
                android:textSize="15sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/advertBookmarkLayout"
            android:layout_width="74dp"
            android:layout_height="45dp"
            android:layout_toRightOf="@+id/advertInfosLayoutLayout" >

            <Button
                android:id="@+id/advertBookmarkIcon"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:layout_centerVertical="true"
                android:background="@drawable/bookmark" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/advertDistanceLayout"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" >

            <Button
                android:id="@+id/advertDistanceIcon"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/distance" />

            <TextView
                android:id="@+id/advertDistanceText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/advertDistanceIcon"
                android:layout_centerHorizontal="true"
                android:text="13 km"
                android:textColor="@color/grey_midark"
                android:textSize="11sp" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

The Java code :

    list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Start new detail activity
        Intent intent = new Intent(activity, AdvertDetailActivity.class);
        startActivity(intent);

        Log.w("AdvertList", "Item clicked");
    }
});

Have you got an idea ? Thanks a lot !

Upvotes: 0

Views: 2101

Answers (1)

Aballano
Aballano

Reputation: 1037

If you're adding buttons/checkboxes to a ListView row, you should add this to them in the xml:

    android:focusable="false"

Why? Because you have 2 click zones, the button and the row itself, so you have to put your row as the principal touch zone.

Try that to see if it works.

On the other hand, you have too many ViewGroups (RelativeLayouts) in your xml, which will make the draw process slower. Having only one (and maybe some LinearLayouts if you really need them) should be enough.

Upvotes: 4

Related Questions