我创建了一个自定义列表视图。我通过子类化 arrayadapter 使用自定义适配器添加了联系人详细信息列表。如果我选择列表中的特定联系人意味着我需要获取所选的详细信息。我怎么能做到这一点。这是我的编码,
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;
}
@Override
public View getView(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);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
row.setTag(holder);
holder.check.setTag(objects[position]);
} else {
holder = (ContactListHolder) row.getTag();
holder.check.setTag(objects[position]);
}
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;
}
}
在 manin 活动中,我使用列表视图作为,
ContactList contactList[] = new ContactList[MyTrackList.size()];
for(int i=0;i<MyTrackList.size();i++) {
MyContact contact = MyTrackList.get(i);
contactList[i] = new ContactList(contact.getName(), contact.getNumber(), contact.getImage());
}
ContactListAdapter adapter = new ContactListAdapter(this, R.layout.manage_track_list_custom_view, contactList);
trackList = (ListView) findViewById(R.id.manage_track_listView);
trackList.setAdapter(adapter);
这里的联系人列表是一个有很多对象的类。
我尝试过这种方式,但没有成功。请指导我。提前致谢。