2012-09-09 49 views
1

我已經作出一個定製ListView但他並沒有叫,我已經註冊了任何的onclick聽衆或上下文菜單。ListView項不能點擊,沒有任何其他的聽衆獲取調用

這裏是我的自定義適配器:

class AdapterContactsActivity extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> rowdata; 
    private static LayoutInflater inflater = null; 
    public ArrayList<String> checkedContacts = new ArrayList<String>(); 

    public AdapterContactsActivity(Activity a, 
     ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     rowdata = d; 
     inflater = (LayoutInflater) activity 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return rowdata.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 

     if (convertView == null) 
      convertView = inflater.inflate(R.layout.item_listview_contacts, 
       null); 
     final ViewHolder holder = new ViewHolder(); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.textView1); 
     holder.chkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     final HashMap<String, String> row = rowdata.get(position); 
     // Setting all values in listview 
     holder.txtTitle.setText(row.get(EmsContactsActivity.KEY_TITLE)); 
     final String contact_id = row.get(EmsContactsActivity.KEY_ID); 
     convertView.setTag(contact_id); 
     holder.txtTitle.setTag(contact_id); 
     if (checkedContacts.contains(contact_id)) 
      holder.chkBox.setChecked(true); 
     else 
      holder.chkBox.setChecked(false); 
     holder.chkBox.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (!holder.chkBox.isChecked() 
      && checkedContacts.contains(contact_id)) 
       checkedContacts.remove(contact_id); 
      else { 
       checkedContacts.add(contact_id); 
      } 
     } 
    }); 
    return convertView; 
    } 
    static class ViewHolder { 
     public TextView txtTitle; 
     public CheckBox chkBox; 
    } 
} 

也是我行的項目視圖:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/checkBox1" 
     android:padding="5dp" 
     android:textAppearance="@android:style/TextAppearance.Large" 
     android:textColor="@android:color/white" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:padding="5dp" /> 

</RelativeLayout> 

現在我已經加入到該適配器的所有聯繫人,但在ListView項目的點擊我甚至不能還得到logcat通知等點擊事件我沒有打電話。

ListView代碼:

Cursor allContacts = getAllContacts(); 
if (allContacts != null && allContacts.getCount() > 0) { 

    txtEmptyText.setVisibility(View.GONE); 
    registerForContextMenu(listViewContacts); 
    listViewContacts.setVisibility(View.VISIBLE); 
    listViewContacts.setFastScrollEnabled(true); 
    listViewContacts.setTextFilterEnabled(true); 
    listViewContacts.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    listViewContacts.setBackgroundColor(Color.TRANSPARENT); 
    searchList = new ArrayList<HashMap<String, String>>(); 
    searchList = getArrayListWithContacts(allContacts); 

    txtTotalContacts.setText("Displaying " + allContacts.getCount() 
       + " contacts"); 

    // Wrap your adapter within the SimpleSectionAdapter 
    searchAdapter = new AdapterContactsActivity(this, searchList); 
    // Set the adapter to your ListView 
    listViewContacts.setAdapter(searchAdapter); 

    listViewContacts.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View v, 
        int position, long arg3) { 
      // TODO Auto-generated method stub 
      Log.i("CLICKED", position + " clicked"); 
    } 
    }); 
} else // No Contacts Found 
{ 
    String text = "<div>You don't have any contacts to display.<br /><br />" 
      + "To add a contacts, <b>Menu</b> and touch:<br /><br />" 
      + "&#8226; <b style='color:#FFFFFF;'>Accounts</b> to add or configure and account with contacts you can sync to the phone<br /><br />" 
      + "&#8226; <b>New Contact</b> to create a new contact from scratch<br /><br />" 
      + "&#8226; <b>Import/Export</b></div>"; 
    txtEmptyText.setVisibility(View.VISIBLE); 
    listViewContacts.setVisibility(View.GONE); 
    txtEmptyText.setText(Html.fromHtml(text)); 
    editSearchContacts.setEnabled(false); 
} 
+0

把你的ListView代碼也 –

+1

嗨Tomer的,看我的編輯問題 –

回答

5

坐落在focusable財產CheckBoxfalse

<CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:padding="5dp" 
     android:focusable="false" /> 

此外,您可能要膨脹排佈置是這樣的:

convertView = inflater.inflate(R.layout.item_listview_contacts, parent, false); 
+1

+1你。它的作品爲這個問題:)但你能給我理由爲什麼它需要使複選框焦點? –

+0

@RushabhPatel你必須設置可對焦屬性爲'CheckBox',否則它將從'ListView'行偷焦點和點擊事件會去'CheckBox'而不是列表行。你會,如果您有其他元素,如要做到這一點按鈕,imagebuttons,等的ToggleButtons OK – Luksprog

+1

Luksprog,非常感謝的人:) –

1
  1. 你的CheckBoxonClickListener所以它可以抓住listview的點擊事件。
  2. 你在你的ListView使用OnItemClickListener當您嘗試處理單擊事件(listView.setOnItemClickListener(...))?
+0

checbox可以使用複選框監聽到你的列表視圖。它不是問題。問題是與複選框。它需要focusable = false。 –

+0

@Rushabh Patel,嘿,我沒有說複選框不能有onclick監聽器。我只是說,在這種情況下'onClickListener'可以抓取listview的事件。這就是爲什麼,正如Luksprog所說的,你應該在複選框描述中加入'android:focusable =「false」'行。 –

+0

現在通過清除您的想法很好 –

1

如果你正在創建列表項目的任何自定義行佈局即可。

Row.xml

在家長線性佈局或相對佈局,你應該把一行

android:descendantFocusability="blocksDescendants" 

這是對我工作的罰款。

相關問題