Dylan Holmes
Dylan Holmes

Reputation: 415

Android ListView setOnItemClickListener not working

I am trying to make a ListView with TextViews, I want each textview to be able to be clicked and based on it's position a Fragment is pulled up. However, the itemOnClickListener seems to not even be called.

        mDrawerList = (ListView) findViewById(R.id.devices_drawer);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, drawerInfo));
    mDrawerList.setItemsCanFocus(false);
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                final int pos, long id) {
            Log.d(TAG, "CLICKED " + pos);
            selectDevice(pos);
        }
    });

here's the xml for the list item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/deviceText"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

List view xml file:

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/devices_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

Upvotes: 1

Views: 6891

Answers (4)

K D
K D

Reputation: 104

For list items inside your list.. you have to set the attributes below

android:focusable="false" android:focusableInTouchMode="false"

Upvotes: 0

Pawan Maheshwari
Pawan Maheshwari

Reputation: 15366

Also check if you are using Button in list row item, it'll over-ride you list item click event

Upvotes: 0

Ivan Bartsov
Ivan Bartsov

Reputation: 21086

Is there a reason you are using mDrawerList.setItemsCanFocus(false);? You don't have any focusable items in your item layout, so it's safe to remove that call. Should work ok after that.

Upvotes: 0

Jordan
Jordan

Reputation: 153

It's your whole xml drawer (I presume you try to do a navigation drawer) ? If you have a parent layout check the android:descendantFocusability="blocksDescendants" otherwise you have to wrap your textview in a layout (LinearLayout is sufficient if you don't have a complex UI for the item). The new OnItemClickListener is an AdapterView.OnItemClickListerner?

(An excellent how-to http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_navigation_drawer )

Upvotes: 5

Related Questions