0

In my fragment i have a listview and when i click would open another activity that correspond at the item in the list.. But actually doesn't work and when i click nothing happen.

@SuppressLint("NewApi")
    protected void onListItemClick(ListView l, View v, int positionItem, long id) {

        Sensor sensor = sensorAdapter.getItem( positionItem ).getSensor();
        String sensorName = sensor.getName();
        Intent i = new Intent(getActivity(), SensorMonitor.class); 
        startActivity(i);
        i.putExtra( "sensorname",sensorName );
        startActivity( i );
    }

Any help? the logCat say nothing if you want to ask me.. I can click but nothing happen.. I also added in the textview of listview:

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

But nothing

EDIT:

This is the custom textview of the listview:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:fontFamily="sans-serif-light"
    android:gravity="center_vertical"
    android:padding="10dp" />

And this is the fragment xml in which there is the listview:

<?xml version="1.0" encoding="utf-8"?> 

        <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" > 

            <!-- Included header.xml here -->
    <TableLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"    
        android:paddingTop="@dimen/activity_vertical_margin"
         >
            <TextView
            android:id="@+id/titolodevicefragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#DB3232"
            android:textSize="18dp"
            android:fontFamily="sans-serif-condensed"
            android:text="@string/titolosensorsfragment" />

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" /> 
         </TableLayout>
        </LinearLayout>
David_D
  • 1,404
  • 4
  • 31
  • 65

5 Answers5

2

try to implement listview.setonItemClickListener. It should definitly work.Otherwise there seems no problem

   list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                int position, long id) {

        }
    });
    }
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • yes..For more details check http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ Hope this helps. :) – Jigar Pandya Nov 26 '13 at 10:28
  • let's see here. i posted the entire code as it is actually: http://stackoverflow.com/questions/20215204/onlistitemclick-not-works-in-the-listview-using-fragment – David_D Nov 26 '13 at 11:00
  • May be inheriting Fragment,use ListFragment thats the reason why I guess your onListItemclick is not being called – Jigar Pandya Nov 26 '13 at 11:15
0

Why are you settings these to false ? :

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

You should set them to true instead

Freego
  • 456
  • 7
  • 18
  • Anyway doesn't work! If i set these to true i can't click on the items..now seems like a textview and not a listview because i can't click on the signle item in the list.. – David_D Nov 26 '13 at 09:54
  • Are you sure that you can't click on an item ? If you're not setting a drawable state to the background of your cells they won't seem to respond to your clicks – Freego Nov 26 '13 at 10:00
0

May be you ListView item contains hasFocusable view.
Please see below link.
https://stackoverflow.com/a/20188525/596555 ,may be help you.

Community
  • 1
  • 1
boiledwater
  • 10,372
  • 4
  • 37
  • 38
0

I have always read to use adapter and the related adapter listener. It's from 2010 IO so maybe it's not the most recent, but I learn my listview here and it works great

Poutrathor
  • 1,990
  • 2
  • 20
  • 44
  • so? Sorry i don't understand – David_D Nov 26 '13 at 10:00
  • I learn to do ListView with adapter and then use `onItemClick()` Video : [link](http://www.youtube.com/watch?v=wDBM6wVEO70) – Poutrathor Nov 26 '13 at 10:01
  • you will hate me, but I suggest you watch the video I link. If you think it is interesting, modify your fragment/activity to follow the Google developer advice and his guidelines. You could also try a bit more things by yourself: your received so many answers! or let it for one day and come back to it tomorrow with fresher mind... – Poutrathor Nov 26 '13 at 10:16
0

Remove these attributes :

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

Add this attribute to your root layout:

android:descendantFocusability="blocksDescendants"

Edit:

Insert in the root LinearLayout

    <?xml version="1.0" encoding="utf-8"?> 

            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
android:descendantFocusability="blocksDescendants" > 

                <!-- Included header.xml here -->
        <TableLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"    
            android:paddingTop="@dimen/activity_vertical_margin"
             >
                <TextView
                android:id="@+id/titolodevicefragment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#DB3232"
                android:textSize="18dp"
                android:fontFamily="sans-serif-condensed"
                android:text="@string/titolosensorsfragment" />

                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" /> 
             </TableLayout>
            </LinearLayout>
Md. Arafat Al Mahmud
  • 3,124
  • 5
  • 35
  • 66