Kevin Panuelos
Kevin Panuelos

Reputation: 13

onListItemClick() is bypassed for ListView with a custom adapter

I realize there are a ton of questions regarding this issue, but the solution of setting every view in the row.xml to focusable="false" and focusableInTouchMode="false" do not work, nor does getting the ListView from the ListActivity and setting setItemsCanFocus(false).

Weirdly, when registered for a context menu, the long tap works. The regular tap though? Nope. I tried setting listeners like OnItemClickListener to no avail.

I read somewhere that I might be able to remedy this by overriding getView() in my Adapter? I'm not too sure how that works though. Note, I don't want to know what view the user has clicked; I just care about the list row being clicked to initiate the corresponding code in onListItemClick().

Maybe there's something in my row.xml that's all wrong? Or is it affected by the way I set my ListView's adapter (placed in onResume() instead of onCreate() to update information)?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:focusable="false"
  android:focusableInTouchMode="false">
<TextView style="?android:attr/listSeparatorTextViewStyle"
  android:id="@+id/listSectionHeader"
  android:layout_width = "fill_parent"
  android:layout_height="wrap_content"
  android:paddingTop="2dp"
  android:paddingBottom="2dp"
  android:paddingLeft="5dp"
  android:textColor="@android:color/white"
  android:visibility="gone"
  android:focusable="false"
  android:focusableInTouchMode="false"/>
<RelativeLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="70dp"
  android:background="@drawable/list_button"
  android:focusable="false"
  android:focusableInTouchMode="false"
  android:clickable="true"
  android:longClickable="true"
  >
<TextView android:id="@+id/itemID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<CheckBox 
    android:id="@+id/returnedCheckbox"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:checked="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />
<LinearLayout
    android:layout_toRightOf="@id/returnedCheckbox"
    android:layout_margin="5dp"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false">
    <TextView 
        android:id="@+id/stuffName"
        android:layout_width="fill_parent"
        android:lines="1"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:scrollHorizontally="true"
        android:text="Hey there"
        android:textSize="25sp"
        android:textColor="#FFF"
        android:shadowColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
    <RelativeLayout
        android:id="@+id/detailsLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false">
        <TextView 
            android:id="@+id/dueListItem"
            android:layout_width="wrap_content"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="@string/dueListing"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <TextView
            android:id="@+id/dueDate"
            android:layout_toRightOf="@id/dueListItem"
            android:layout_marginLeft="2dp"
            android:layout_width="90dp"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="HEHEHE"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <ImageView
            android:id="@+id/starMark"
            android:layout_alignRight="@id/detailsLayout"
            android:layout_toRightOf="@id/dueDate"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="2dp"
            android:layout_height="15dp"
            android:layout_width="15dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/list_starred"
            android:visibility="invisible"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
    </RelativeLayout>
</LinearLayout>
<ImageView 
        android:id="@+id/contactPic"
        android:layout_alignParentRight="true"
        android:layout_height="48dp"
        android:layout_width="48dp"
        android:background="#FFF"
        android:layout_margin="10dp"
        android:layout_centerVertical="true"
        android:scaleType="fitCenter"
        android:padding="3dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
<ImageView 
        android:id="@+id/lentArrow"
        android:visibility="invisible"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/indicator_lent"
        android:layout_marginTop="42dp"
        android:layout_marginRight="1dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>

Upvotes: 1

Views: 2827

Answers (3)

Kevin Panuelos
Kevin Panuelos

Reputation: 13

I didn't have to do anything to my adapter; onListItemClick() just miraculously worked on its own for some reason. I'm scratching my head even more right now.

Incidentally, I extended CursorAdapter, not SimpleCursorAdapter, which I understand is sort of different. From what I read, CursorAdapter extension requires only overriding the newView() and bindView() instead of the usual getView() based on this http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/.

Upvotes: 0

Ron
Ron

Reputation: 24235

The Onlistitemclick will never be called because there is a clickable view in your list item. Remove the checkbox and see if you are able to get the clicks.

There is an alternative to using using a checkbox directly in your item layout. Use android:choiceMode="multipleChoice" for listview if it suits your needs.

Upvotes: 1

hooked82
hooked82

Reputation: 6376

Take a look at this link in how to create a custom adapter:

http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html

In the Override getView() method, you could just set the row.setOnClickListener() to do what you needed to do when that list item is clicked.

Ideally, you would use convertView to fill a ViewHolder class so that you aren't recreating list items when they have already been created, but that's a different question.

Edit

Here's a slimmed down custom implementation of extending a SimpleCursorAdapter:

public class CatchCursorAdapter extends SimpleCursorAdapter {

    private final Context mContext;
    private final Cursor mCursor;
    private final int layout;
    private final LayoutInflater inflater;

    private final class ViewHolder {
        public ImageView catchImage;
        public ImageView catchStar;
        public TextView catchSpecies;
    }

    public CatchCursorAdapter(Context context, int layout, Cursor cursor) {
        super(context, layout, cursor, new String[] { }, new int[] { });

        this.mContext = context;
        this.mCursor = cursor;
        this.inflater = LayoutInflater.from(context);
        this.layout = layout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(layout, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.catchImage = (ImageView)convertView.findViewById(R.id.catch_image);
            viewHolder.catchSpecies = (TextView)convertView.findViewById(R.id.catch_species);
            viewHolder.catchStar = (ImageView)convertView.findViewById(R.id.catch_starfavorite);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        convertView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Perform an action when this list item is clicked
            }
        });
        viewHolder.catchStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //You can also have buttons within your list item that can be clicked
                //independently of the parent list item.
            }
        });

        //Set the rest of your views

        return convertView;
    }
}

Upvotes: 0

Related Questions