下面是我的适配器类,我在其中检索电话联系人,尝试使用复选框获取多个联系人。
public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) {
super(activity, textViewResourceId, listCont);
this.renderer = renderer;
this.listCont = listCont;
this.activity = activity;
this.profileType=profileType;
checkBoxState=new boolean[listCont.size()];
}
private class ViewHolder
{
ImageView photo;
TextView text,textContNo, textEmailId;
CheckBox checkBox;
}
@TargetApi(9)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) (getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
view = inflater.inflate(renderer, null);
viewHolder=new ViewHolder();
//cache the views
viewHolder.photo=(ImageView) view.findViewById(R.id.photo);
viewHolder.text=(TextView) view.findViewById(R.id.name);
viewHolder.textContNo=(TextView) view.findViewById(R.id.contactno);
viewHolder.textEmailId=(TextView) view.findViewById(R.id.emailId);
viewHolder.checkBox=(CheckBox) view.findViewById(R.id.checkBox1);
viewHolder.checkBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Profile element = (Profile) viewHolder.checkBox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkBox.setTag(listCont.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkBox.setTag(listCont.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(listCont.get(position).getName());
holder.checkBox.setChecked(listCont.get(position).isSelected());
holder.text = (TextView) view.findViewById(R.id.name);
holder.photo = (ImageView) view.findViewById(R.id.photo);
holder.textContNo = (TextView) view.findViewById(R.id.contactno);
holder.textEmailId = (TextView) view.findViewById(R.id.emailId);
Profile contact = listCont.get(position);
holder.text.setText(contact.getName());
contact.getName();
contact.getId();
holder.text.setTag(contact);
holder.text.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Profile myContact= (Profile) v.getTag();
Intent intent = new Intent();
intent.putExtra("type", profileType);
intent.putExtra("name", myContact.getName());
intent.putExtra("email", myContact.getEmail());
intent.putExtra("contactid", myContact.getId());
intent.putExtra("address", myContact.getAddress());
intent.putExtra("city", myContact.getCity());
intent.putExtra("state", myContact.getState());
intent.putExtra("countryName", myContact.getCountryName());
intent.putExtra("postalCode", myContact.getPostalCode());
intent.putExtra("website", myContact.getWebSite());
intent.putExtra("mobileNumber", myContact.getMobileNo());
intent.putExtra("phoneNumber", myContact.getLandLineNo());
Log.d(TAG, "On Activity Result Method : 1");
activity.setResult(100, intent);
activity.finish();
Constants.loadEntries.cancel(true);
return false;
}
});
if(contact.getPhoto() !=null && !contact.getPhoto().equals("")){
viewHolder.photo.setImageBitmap(contact.getPhoto());
}else{
viewHolder.photo.setImageResource(R.drawable.profile);
}
holder.photo.setTag(contact);
holder.photo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Profile myContact= (Profile)v.getTag();
Intent intent = new Intent();
intent.putExtra("type", profileType);
intent.putExtra("name", myContact.getName());
intent.putExtra("email", myContact.getEmail());
intent.putExtra("contactid", myContact.getId());
intent.putExtra("address", myContact.getAddress());
intent.putExtra("website", myContact.getWebSite());
intent.putExtra("mobileNumber", myContact.getMobileNo());
intent.putExtra("phoneNumber", myContact.getLandLineNo());
Log.d(TAG, "On Activity Result Method : 2");
activity.setResult(100, intent);
activity.finish();
Constants.loadEntries.cancel(true);
return false;
}
});
holder.textContNo.setText(contact.getNumber());
holder.textEmailId.setText(contact.getEmail());
view.setClickable(true);
view.setTag(contact);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Profile myContact= (Profile) v.getTag();
Intent intent = new Intent();
intent.putExtra("type", profileType);
intent.putExtra("name", myContact.getName());
intent.putExtra("email", myContact.getEmail());
intent.putExtra("contactid", myContact.getId());
intent.putExtra("address", myContact.getAddress());
intent.putExtra("website", myContact.getWebSite());
intent.putExtra("mobileNumber", myContact.getMobileNo());
intent.putExtra("phoneNumber", myContact.getLandLineNo());
Log.d(TAG, "On Activity Result Method : 3");
activity.setResult(100, intent);
activity.finish();
Constants.loadEntries.cancel(true);
}
});
LinearLayout layout = (LinearLayout)view.findViewById(R.id.profilelayout);
layout.setTag(contact);
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Profile myContact= (Profile) v.getTag();
Intent intent = new Intent();
intent.putExtra("type", profileType);
intent.putExtra("name", myContact.getName());
intent.putExtra("phoneNumber", myContact.getLandLineNo());
intent.putExtra("email", myContact.getEmail());
intent.putExtra("contactid", myContact.getId());
intent.putExtra("address", myContact.getAddress());
intent.putExtra("website", myContact.getWebSite());
intent.putExtra("mobileNumber", myContact.getMobileNo());
Log.d(TAG, "On Activity Result Method : 4");
activity.setResult(100, intent);
activity.finish();
Constants.loadEntries.cancel(true);
}
});
if (position % 2 == 0)
view.setBackgroundResource(R.drawable.listshape);
else
view.setBackgroundResource(R.drawable.favoritebody);
if (contact.getAddress() != null && contact.getAddress().length() != 0){
view.setBackgroundResource(R.drawable.hasaddress);
}
return view;
}
}
当我选中一个复选框并滚动复选标记重复几行时,如何避免这种情况,我在行出现 gettign classcast 异常
((ViewHolder) view.getTag()).checkBox.setTag(listCont.get(position));
任何帮助表示赞赏。