我做了一个自定义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 />"
+ "• <b style='color:#FFFFFF;'>Accounts</b> to add or configure and account with contacts you can sync to the phone<br /><br />"
+ "• <b>New Contact</b> to create a new contact from scratch<br /><br />"
+ "• <b>Import/Export</b></div>";
txtEmptyText.setVisibility(View.VISIBLE);
listViewContacts.setVisibility(View.GONE);
txtEmptyText.setText(Html.fromHtml(text));
editSearchContacts.setEnabled(false);
}