无法单击ListView项,也不会调用任何其他侦听器

时间:2012-09-09 11:40:21

标签: android android-listview custom-adapter

我创建了一个自定义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);
}

3 个答案:

答案 0 :(得分:5)

focusable上的CheckBox属性设置为false

<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)

  1. 您的CheckBoxonClickListener,因此可以抓取listview的点击事件。
  2. 当您尝试处理点击事件(OnItemClickListener)时,您是否在ListView上使用了listView.setOnItemClickListener(...)

答案 2 :(得分:1)

如果您要为列表项创建任何自定义行布局,则<。p>

内部Row.xml

在Parent Linear Layout或Relative LAyout中,你应该放一行

android:descendantFocusability="blocksDescendants" 

这对我来说很好。