Michał Górecki
Michał Górecki

Reputation: 31

ListView onItemClick not called

I have a ListView with a custom adapter and list items containing TextView(s) only. The list items have an OnItemClick method set in the onCreate callback method.

 templatesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(DEBUG_TAG, "templatesListView onClick()");
            //item is selected from the cursor to get necessary data
            Log.d(DEBUG_TAG, "ListView count: " + templatesListView.getCount());

            Log.d(DEBUG_TAG, "messagesCursor count: " + messagesCursor.getCount());

            if (position >= messagesCursor.getCount()) {
                Log.d(DEBUG_TAG, "Unable to access element " + position + ", it does not exist in the messagesCursor. Cursor count: " + messagesCursor.getCount());
            }

            messagesCursor.moveToPosition(position);
            final String selectedItemName = messagesCursor.getString(1);

            AlertDialog.Builder builder = new AlertDialog.Builder(SendMessageActivity.this);
            builder.setTitle(selectedItemName).setMessage("Do you want to use template: "+selectedItemName+"?");

            //Use template onClick
            builder.setPositiveButton("Use", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dlg, int x) {
                    messageEditText.setText(selectedItemName);
                }
            });

            //Cancel onClick
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dlg, int x) {
                }
            });
            builder.show();
        }
    });

The ListView in the activity layout file is defined as:

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/templatesListView"
    android:layout_alignParentRight="true"
    android:clickable="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/sendButton" />

The list item is defined in a separate layout file as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/name_textView" />

The onClick method is called correctly when I run the app on Android 4.4.4, but when I run it on Android 5.1.1 it is not called at all.

The list item layout has been also created for v21+ separately, please find the code below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Medium Text"
    android:id="@+id/name_textView"
    android:singleLine="true"
    android:textColor="@color/foreground_material_light"
    android:theme="@android:style/Widget.Material.Light.Button.Borderless" />

Do you guys know what should I change to make it work on API level 21+ ? Is that a matter of the xml file only (attributes?) or should I change the implementation? Cheers!

Upvotes: 0

Views: 100

Answers (3)

Michał G&#243;recki
Michał G&#243;recki

Reputation: 31

SOLVED: I think the additional layout file was confusing for the application. It was named exactly the same as the original one, but with (v21) added at the end of it. After removing the second file it works fine.

Upvotes: 1

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

Looking at this closely related problem and the selected answer, I think you should make some changes to your code.

Seeing that you have the LinearLayout which has a ListView as a child view - and this ListView has the LinearLayout as a child, which in turn has TextView as a child? In this case, perhaps the child views of your ListView should have the onClickListener. This means setting the following attributes to your name_textView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false"
    android:id="@+id/name_textView" />

Then keep the templatesListView as :

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/templatesListView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/sendButton" />

And finally, change your list-item linear layout as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:weightSum="1">

Give it a try and let me know if this helps.

Upvotes: 0

Aritra Roy
Aritra Roy

Reputation: 15615

Just remove this,

android:clickable="true"

from your ListView. It is actually blocking the clicks from getting transferred to the individual rows.

Upvotes: 1

Related Questions