0

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

@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的说什么,如果你要问我..我可以点击,但没有发生。我在列表视图的TextView中还增加了:

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

但没有

编辑:

这是列表视图的自定义的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片段,其中有列表视图:

<?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> 
+0

机器人:focusableInTouchMode =“假” 机器人:可点击=“假” 机器人:可调焦=“假”使所有真正的.. – Avijit

+0

现在我不能点击该项.. –

+0

而不是getActivity()在意图使用getApplicationContext() – Avijit

回答

2

尝试实现listview.setonItemClickListener。应该definitly work.Otherwise似乎没有问题

list.setOnItemClickListener(new OnItemClickListener() { 

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

     } 
    }); 
    } 
+0

? –

+0

是的..欲了解更多详情,请查看http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/希望这会有所帮助。 :) –

+0

不工作!也许我错了什么.. –

0

你为什么把这些设置为false? :

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

你应该将它们设置为代替

+0

反正不行!如果我将这些设置为true,我不能点击这些项目..现在看起来像是一个textview,而不是一个列表视图,因为我无法点击列表中的标志项。 –

+0

您确定无法点击在一个项目上?如果您没有为单元格的背景设置可绘制状态,那么它们似乎不会响应您的点击 – Freego

0

我一直阅读使用适配器和相关的适配器侦听器。这是自2010年IO虽然这也许不是最新的,但我学我的ListView here和它的伟大工程

+0

?对不起,我不明白 –

+0

我学习使用适配器做ListView,然后使用'onItemClick()'Video:[link](http://www.youtube.com/watch?v=wDBM6wVEO70) – Poutrathor

+0

那么你的建议是什么?你能帮助我,因为我找不到出路...... –

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> 
+0

'android:descendantFocusability =“blocksDescendants”'在自定义textview而不是其他人或在另一个布局? –

+0

给出的XML代码,我会告诉你在哪里插入 –

+0

让我们看看我的编辑..我已经删除了其他人 –