bos
bos

Reputation: 6535

ListView element not clickable

I have a ListView with SimpleCursorAdapter. The layout was using a LinearLayout, but when reading manuals about memory consumption for complex (nested) layouts, I switched to RelativeLayout instead and I've managed to setup the layout exactly as I want.

Now, there's one quirk. In the emulator, I cannot click the list items. It seems as if the elements are one big "no button". However, if I use the emulator arrows and select the listview element (highlights it) and click the button, it works fine.

Why can't I "click" the listview items since I switched to RelativeLayout?

Here's the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

  <TextView
    android:id="@+id/locationName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Junk text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
    android:id="@+id/numStores"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/locationName"
    android:text="Junk text #1: 117"
    android:textSize="10dp" />

  <TextView
    android:id="@+id/numItems"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/numStores"
    android:text="Junk text #2: 42"
    android:textSize="10dp" />
</RelativeLayout>

I even tried android:clickable="true" for RelativeLayout, to no avail.

EDIT Code for onClick is as follows:

listItems.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    int countyID = cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID));
    int numItems = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_ITEMS));
    int numStores = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_STORES));
    String countyName = cursor.getString(cursor.getColumnIndex(LocationDB.C_NAME));
    String foo = String.format(TAG + "Clicked ID #%d (%s), stores = %d, items = %d", countyID, countyName, numStores, numItems);
    Log.i(LOG_TAG, foo);

    // Show the item in a new activity
    Intent apan = new Intent(avBase, Browser.class);
    apan.putExtra("countyID", countyID);
    startActivity(apan);
  }
});

EDIT 2

The code is now tested on my phone, and I get the same error; can't click the items when using RelativeLayout.

Upvotes: 0

Views: 3333

Answers (2)

JimmyB
JimmyB

Reputation: 12610

I faced some issues with this, too, which seemed to be related to the combination of the ListView's on click listeners and those I assigned each individual item in my Adapter.

In my Adapter.getView() I had to make sure the items were configured like:

item.setLongClickable( false );
item.setClickable( false );
item.setOnClickListener( null ); 

while my ListView had to have its OnItemClickListener and/or its OnItemLongClickListener set.

Turning on clicks on the individual items and on the listview would not work. Maybe that's what's causing your issue, too.

Upvotes: 4

bos
bos

Reputation: 6535

Seems this is a bug that Google ignores: http://code.google.com/p/android/issues/detail?id=3414

Upvotes: 1

Related Questions