Question

My android app has a List View. That list view has a lot of items. The objective is to call an intent, but give it a different extra depending on te item.

The problem I have is that when i click any item, I see the yellow highlight,but the on click is not called. I researched a bit and i know it has to do with the layout, and the two text views being clicked, and not the menu item, but i don't know how to solve this.

I tried using android:focusable="false" and android:clickable="false" on everything, but it didn't solve my problem.

lvRoot.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Intent intent = new Intent(getApplicationContext(), Word.class);
            intent.putExtra("INDEX",
                    MainActivity.getSearchResultsIndex()[arg2]);
            startActivity(intent);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

This is the xml for the menu items:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/menuItem"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:clickable="false"
android:focusable="false"
android:orientation="horizontal"
tools:context=".Search" >

<TextView
    android:id="@+id/item1"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight=".5"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center_vertical"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/item2"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight=".5"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center_vertical"
    android:textColor="@android:color/white" />

Thanks in advance for any help, if you need anymore of my code, just say.

Was it helpful?

Solution

You want OnItemClickListener and not OnItemSelectedListener

lvRoot.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent intent = new Intent(getApplicationContext(), Word.class);
        intent.putExtra("INDEX", MainActivity.getSearchResultsIndex()[position]);
        startActivity(intent);
    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top