我无法在Android中单击ListView?

前端之家收集整理的这篇文章主要介绍了我无法在Android中单击ListView?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用联系人同步创建了一个应用.我列出了以下联系信息,包括照片,姓名和号码.我成功地在自定义ListView中列出了所有这些东西,但我无法单击ListView.它看起来像锁定,无法点击它.

但我对另一项活动做了同样的程序.使用自定义ListView,但我可以单击此视图,它工作正常.

问题是什么?这是我的示例编码:

  1. ListView settingsList = (ListView) findViewById(R.id.manage_track_listView);
  2. ArrayList<ContactList> MySettingsList = new ArrayList<ContactList>();
  3.  
  4. ContactList setting1 = new ContactList("contact name 1","Number 1",null);
  5. ContactList setting2 = new ContactList("contact name 2","Number 2",null);
  6. ContactList setting3 = new ContactList("contact name 3","Number 3",null);
  7.  
  8. MySettingsList.add(setting1);
  9. MySettingsList.add(setting2);
  10. MySettingsList.add(setting3);
  11.  
  12. ContactList list[] = new ContactList[MySettingsList.size()];
  13.  
  14. for(int i=0;i<MySettingsList.size();i++) {
  15.  
  16. ContactList mySettings = MySettingsList.get(i);
  17. list[i] = new ContactList(mySettings.getName(),mySettings.getNumber(),mySettings.getImageIcon());
  18. }
  19.  
  20. ContactListAdapter adapter = new ContactListAdapter(this,R.layout.manage_track_list_custom_view,list);
  21. settingsList.setAdapter(adapter);
  22. System.out.println("before listener");
  23. settingsList.setOnItemClickListener(new OnItemClickListener() {
  24.  
  25. @Override
  26.  
  27.  
  28. public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
  29. // TODO Auto-generated method stub
  30.  
  31. System.out.println("Clicked " + position);
  32. }
  33. });
  34. System.out.println("after listener");

这里ContactList是一个类,它具有imageBlob的联系人姓名,号码和字节[].如果图像为空,我将默认ic_launcher设置为联系人图像.适配器类是:

  1. public class ContactListAdapter extends ArrayAdapter<ContactList> {
  2.  
  3. Context context;
  4. int layoutResourceId;
  5. ContactList objects[] = null;
  6.  
  7. View row;
  8.  
  9. public ContactListAdapter(Context context,int layoutResourceId,ContactList[] objects) {
  10. super(context,layoutResourceId,objects);
  11. // TODO Auto-generated constructor stub
  12.  
  13. this.context = context;
  14. this.layoutResourceId = layoutResourceId;
  15. this.objects = objects;
  16. System.out.println(objects[1].getName());
  17. System.out.println(objects[1].getNumber());
  18. System.out.println(objects[1].getImageIcon());
  19. }
  20.  
  21. @Override
  22. public View getView(final int position,View convertView,ViewGroup parent) {
  23. // TODO Auto-generated method stub
  24.  
  25. row = convertView;
  26. final ContactListHolder holder;
  27.  
  28. if ( row == null ) {
  29.  
  30. LayoutInflater inflater = ((Activity)context).getLayoutInflater();
  31. row = inflater.inflate(layoutResourceId,parent,false);
  32.  
  33. holder = new ContactListHolder();
  34. holder.image = (ImageView) row.findViewById(R.id.contactImage);
  35. holder.name = (TextView) row.findViewById(R.id.contactName);
  36. holder.number = (TextView) row.findViewById(R.id.contactNumber);
  37. holder.check = (CheckBox) row.findViewById(R.id.selectedContact);
  38.  
  39. row.setTag(holder);
  40.  
  41. } else {
  42.  
  43. holder = (ContactListHolder)row.getTag();
  44. }
  45.  
  46. ContactList contact = objects[position];
  47. if(contact.imageIcon != null) {
  48.  
  49. Bitmap imgBitmap = BitmapFactory.decodeByteArray(contact.imageIcon,contact.imageIcon.length);
  50. holder.image.setImageBitmap(imgBitmap);
  51. } else {
  52.  
  53. holder.image.setImageResource(R.drawable.ic_launcher);
  54. }
  55.  
  56. holder.name.setText(contact.name);
  57. holder.number.setText(contact.number);
  58. holder.check.setChecked(objects[position].isSelected());
  59.  
  60. return row;
  61.  
  62. }
  63.  
  64. static class ContactListHolder {
  65.  
  66. ImageView image;
  67. TextView name;
  68. TextView number;
  69. CheckBox check;
  70. }
  71. }

我有超过100个联系人,所以只添加了3个对象.在此联系人列表中,我成功收到联系人图片,姓名,电话号码.

ListView无法单击的问题是什么?我希望你们中的任何一个人能指导我.提前致谢.

谢谢大家.现在通过在我的所有子视图中添加android:focusable =“false”得到了结果.谢谢你的指导.

解决方法

在嵌套视图中,子视图始终首先获取所有触摸事件.如果你想要父视图(在你的情况下,listView行),要获得触摸事件,你必须在子事件上返回false或者在清单中将它们设置为android:clickable =“false”.

猜你在找的Android相关文章