1

我做了一个自定义ListView,但它没有调用我注册的任何 onclick 侦听器或上下文菜单。

这是我的自定义适配器:

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

3 回答 3

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);
于 2012-09-09T11:48:06.177 回答
1
  1. CheckBoxonClickListener,所以它可以获取 listview 的点击事件。
  2. 当您尝试处理点击事件时,您是否正在使用OnItemClickListener您的( ) ?ListViewlistView.setOnItemClickListener(...)
于 2012-09-09T11:55:35.953 回答
1

如果您正在为列表项创建任何自定义行布局,那么。

里面 Row.xml

在父线性布局或相对布局中,您应该放置一行

android:descendantFocusability="blocksDescendants" 

这对我来说很好。

于 2013-04-18T15:22:18.510 回答