onListItemClick侦听器不起作用

时间:2013-11-26 09:47:02

标签: android android-listview android-fragmentactivity onitemclicklistener

在我的片段中,我有一个列表视图,当我点击时会打开列表中项目对应的另一个活动..但实际上不起作用,当我点击什么都没有发生。

@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 );
    }

有任何帮助吗?如果你想问我,logCat什么都不说..我可以点击但没有任何事情发生..我也在listview的textview中添加:

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

但没什么

编辑:

这是listview的自定义textview:

<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" />

这是片段xml,其中有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>

5 个答案:

答案 0 :(得分:2)

尝试实现listview.setonItemClickListener。它应该绝对有用。否则似乎没有问题

   list.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });
    }

答案 1 :(得分:0)

为什么要将这些设置为false? :

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

您应该将它们设置为true

答案 2 :(得分:0)

可能是ListView项目包含hasFocusable视图 请参阅以下链接 https://stackoverflow.com/a/20188525/596555,可能对您有所帮助。

答案 3 :(得分:0)

我一直在阅读使用适配器和相关的适配器监听器。它来自2010 IO,所以也许它不是最新的,但我学习了我的listview here并且它很有效

答案 4 :(得分:0)

删除这些属性:

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

将此属性添加到根布局:

机器人:descendantFocusability = “blocksDescendants”

修改

插入根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>