codeman
codeman

Reputation: 9028

ListView OnItemClickListener not working on second adapter

I have a fragment with a ListView and two buttons above this ListView. The two buttons call the following methods which reload the ListView with a different adapter for the data source. The problem is with the second one. When calling the showWhenWhere(), the onItemClick() is never fired.

private void showAtoZ() {
        lineupAdapter = new LineupAdapter(parentView.getContext());
        listView.setAdapter(lineupAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // ListView Clicked item index
                System.out.println("A to Z tapped");
                int itemPosition     = position;
                if(parentActivity != null) {
                    parentActivity.changeFragment(new BandFragment());
                }
            }
        });
        loadData();
    }

    private void showWhenWhere() {
        whenWhereAdapter = new LineupWhenWhereAdapter(parentView.getContext());
        listView.setAdapter(whenWhereAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // ListView Clicked item index
                System.out.println("When/Where tapped");
                int itemPosition     = position;
                if(parentActivity != null) {
                    parentActivity.changeFragment(new BandFragment());
                }
            }
        });
        loadData();
    }

UPDATE - I determined it's caused by the XML for the whenWhereAdapter, which I'm pasting below:

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bandImageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        android:background="@android:color/darker_gray" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/textView"
        android:background="@drawable/list_row_bg" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Elijah &amp; The Moon"
        android:id="@+id/bandTitle"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="30dp"
        android:textSize="23sp"
        android:maxLines="2"
        android:textColor="@android:color/white" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/black">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12:00 PM"
            android:id="@+id/whenLabel"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:textColor="@android:color/holo_orange_light"
            android:textSize="12sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="|"
            android:id="@+id/divider"
            android:layout_toRightOf="@+id/whenLabel"
            android:layout_toEndOf="@+id/whenLabel"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginBottom="6dp"
            android:layout_alignParentBottom="true"
            android:textIsSelectable="true"
            android:textSize="13sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="West Stage"
            android:id="@+id/whereLabel"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/divider"
            android:layout_toEndOf="@+id/divider"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:textSize="12sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD TO SCHEDULE"
            android:id="@+id/addButton"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@android:color/transparent"
            android:drawableLeft="@drawable/add_to_schedule"
            android:drawablePadding="5dp"
            android:textColor="@android:color/holo_orange_light"
            android:layout_marginRight="10dp" />
    </RelativeLayout>

</RelativeLayout>

Any idea why this would cause the OnItemClickListener to not work?

Upvotes: 0

Views: 221

Answers (1)

pozuelog
pozuelog

Reputation: 1314

ListView only maintains one OnItemClickListener at time. When you set the second listener, only that listener will be called.

-------EDIT -------

After seeing your row layout, I think you should set android:descendantFocusability="blocksDescendants" on your very first RelativeLayout.

That happen because TextViews && Buttons inside layout are blocking click event.

Upvotes: 2

Related Questions