我做了一個應用程序使用聯繫人同步。我列出了以下聯繫信息與照片,名稱和編號。我成功列出所有這些東西在自定義ListView,但我不能單擊ListView。它看起來像鎖定,無法點擊它。我無法在android中單擊ListView?
但我對另一個活動做了同樣的過程。使用自定義ListView但我可以點擊這個視圖,它工作正常。
什麼問題?這裏是我的示例代碼:
ListView settingsList = (ListView) findViewById(R.id.manage_track_listView);
ArrayList<ContactList> MySettingsList = new ArrayList<ContactList>();
ContactList setting1 = new ContactList("contact name 1", "Number 1", null);
ContactList setting2 = new ContactList("contact name 2", "Number 2", null);
ContactList setting3 = new ContactList("contact name 3", "Number 3", null);
MySettingsList.add(setting1);
MySettingsList.add(setting2);
MySettingsList.add(setting3);
ContactList list[] = new ContactList[MySettingsList.size()];
for(int i=0;i<MySettingsList.size();i++) {
ContactList mySettings = MySettingsList.get(i);
list[i] = new ContactList(mySettings.getName(), mySettings.getNumber(), mySettings.getImageIcon());
}
ContactListAdapter adapter = new ContactListAdapter(this, R.layout.manage_track_list_custom_view, list);
settingsList.setAdapter(adapter);
System.out.println("before listener");
settingsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("Clicked " + position);
}
});
System.out.println("after listener");
這裏ContactList是一個類,它有imageBlob的聯繫人姓名,號碼和字節[]。如果圖像爲空,我將默認的ic_launcher設置爲聯繫人圖像。適配器類是:
public class ContactListAdapter extends ArrayAdapter<ContactList> {
Context context;
int layoutResourceId;
ContactList objects[] = null;
View row;
public ContactListAdapter(Context context, int layoutResourceId, ContactList[] objects) {
super(context, layoutResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.layoutResourceId = layoutResourceId;
this.objects = objects;
System.out.println(objects[1].getName());
System.out.println(objects[1].getNumber());
System.out.println(objects[1].getImageIcon());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
row = convertView;
final ContactListHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactListHolder();
holder.image = (ImageView) row.findViewById(R.id.contactImage);
holder.name = (TextView) row.findViewById(R.id.contactName);
holder.number = (TextView) row.findViewById(R.id.contactNumber);
holder.check = (CheckBox) row.findViewById(R.id.selectedContact);
row.setTag(holder);
} else {
holder = (ContactListHolder)row.getTag();
}
ContactList contact = objects[position];
if(contact.imageIcon != null) {
Bitmap imgBitmap = BitmapFactory.decodeByteArray(contact.imageIcon, 0, contact.imageIcon.length);
holder.image.setImageBitmap(imgBitmap);
} else {
holder.image.setImageResource(R.drawable.ic_launcher);
}
holder.name.setText(contact.name);
holder.number.setText(contact.number);
holder.check.setChecked(objects[position].isSelected());
return row;
}
static class ContactListHolder {
ImageView image;
TextView name;
TextView number;
CheckBox check;
}
}
我有超過100個聯繫人,所以只添加了3個對象。在此聯繫人列表中,我成功地收到了聯繫人圖片,姓名,號碼。
什麼問題ListView無法點擊?我希望你中的任何一個人都會引導我。提前致謝。
謝謝大家。現在只需在我的所有子視圖中添加android:focusable="false"
即可獲得結果。感謝你的指導。
此方法不起作用?任何其他解決方案 – Amarnath
現在感謝朋友的工作。 – Amarnath
如果它的工作可以自由接受答案 – thepoosh