2010-06-24 55 views
4

你好,我已經閱讀了很多有關Android中的CheckBox/ListView問題。所以我嘗試了很多問題。使用虛假可調焦複選框,仍然可以防止listview點擊

開始我的行佈局看起來像這樣。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <CheckBox 
      android:id="@+id/check" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="" /> 
    </LinearLayout> 

於是我嘗試添加這對我的ListActivity

ListView listview = getListView(); 
listview.setItemsCanFocus(false); 

然後試圖與onListItemClick斷點,仍然沒有命中(行進路線調試)運行它。

這是我的onListItemClick,以防您想要查看。

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

     // let's find the checkbox we're on. 
     CheckBox targetCheckBox = (CheckBox) l.findViewById(R.id.check); 

     // ok update the database with the new data. 
     mDbHelper.updateNote(id, !targetCheckBox.isChecked()); 

     // update the list now. 
     showList(); 

    } 

如果我再更改複選框CheckTextView,它的工作,但是我從來沒有這樣做之前,我寧願弄清楚到底是什麼錯在這裏是當其他人已經解決了這一點。有什麼想法嗎?

+0

一旦你調用了'onListItemClick()',你的代碼就被破壞了。你不要在'ListView'上調用'findViewById()'來獲得你的'CheckBox',你可以在'View''上使用'findViewById()','View'就是'View'行。 – CommonsWare 2010-06-25 00:44:16

+0

啊,謝謝你。然而,我在該行的斷點甚至沒有被觸及,所以我沒有機會調試該代碼(除了它被checktextview打中)。 – Kinglink 2010-06-25 20:24:20

回答

12

顯然我錯過

android:clickable="false" 

除了複選框下

android:focusable="false" 

正確添加兩條線,使onListItemClick火災。