2014-05-23 44 views
-1

這是我的代碼:setOnItemClickListener不工作在一個ListView

的ListView:

ListView lv; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     final View rootView = inflater.inflate(R.layout.aller_layout, container, false); 
     lv = (ListView) rootView.findViewById(R.id.listView1); 
     CustomAdapter ca = new CustomAdapter(rootView.getContext(), R.layout.preflist_checkbox_layout, oggetti); 
     lv.setAdapter(ca); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id){ 

       Log.i("onclick", "si"); 

       Toast.makeText(view.getContext(), "You clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     return rootView; 
    } 

和適配器:

public class CustomAdapter extends ArrayAdapter<Oggetto>{ 

    private Context context; 
    private Oggetto[] oggetti = null; 
    private int layoutid; 

    public CustomAdapter(Context context, int layoutid, Oggetto[] oggetti){ 
     super(context, layoutid, oggetti); 

     this.context = context; 
     this.oggetti = oggetti; 
     this.layoutid = layoutid; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView==null){ 
      LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
      convertView = inflater.inflate(layoutid, parent, false); 
     } 

     Oggetto oggetto = oggetti[position]; 
     CheckBox cb = (CheckBox) convertView.findViewById(R.id.pref_checkbox); 
     cb.setText(oggetto.nome); 
     cb.setChecked(oggetto.preferito == 1); 
     return convertView; 
    } 
} 

現在我只是想趕上點擊列表視圖中的複選框,但代碼不工作,因爲它應該,當我點擊任何元素時,Log或Toast都不顯示。

我哪裏錯了?

編輯:因爲好像是在XML的一個問題,我將添加ListView控件行的佈局:

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

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

    <CheckBox 
     android:id="@+id/pref_checkbox" 
     android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:gravity="center_vertical" 
     android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
     android:paddingLeft="6dip" 
     android:paddingRight="6dip" 
     android:focusable="false" 
     android:focusableInTouchMode="false"/> 

</LinearLayout> 

回答

0

好吧,我設法找到問題。

我無法做出onItemClick作品,我想這是因爲它實際上根本不適用於複選框(或者至少我無法設法找到一種方法)。

因此,我將一個onClickListener鏈接到適配器內的複選框,它的工作原理類似於一個魅力。

+0

最後我最後的評論爲你工作。 – Meghna

+0

不,這是錯的。即使使用複選框,您也可以單擊列表項並選中複選框。 – Raghunandan

+0

爲一個例子檢查這個http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button。接受錯誤的答案 – Raghunandan

0

我猜CheckBox需要當你在列表項單擊

有這種

重點
android:descendantFocusability="blocksDescendants" 

根元素在preflist_checkbox_layout.xml

+0

仍然無法正常工作,我使用xml代碼編輯了帖子。 – Higure

+0

@Higure我看不出有什麼理由不起作用。你不需要這個'android:focusable =「false」android:focusableInTouchMode =「false」' – Raghunandan

+0

我已經記下了這兩行,它仍然不能工作,我點擊了,但Log或Toast都沒有出現。 – Higure

0

問題在於Android不允許您選擇具有可聚焦元素的列表項。收藏此像這樣:

android:focusable="false" 
0

這些屬性添加到您的複選框:

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

不改變任何東西。 – Higure

0
lv.setOnItemClickListener(this); 
    @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      Toast toast = Toast.makeText(getApplicationContext(), 
        "Item " + (position + 1) + ": ",Toast.LENGTH_SHORT); 

     } 

試試這個

+0

這會造成什麼區別? – Raghunandan

+0

沒有什麼大的區別,他有單獨的onItemClickListen類,我只是在同一個類中添加的。因爲我看到了其他解決方案。這些解決方案應該可以工作,但不能工作,所以它可能會有所幫助。 – Meghna

+0

這完全沒有區別。 op具有匿名內部類。它會工作 – Raghunandan

相關問題