Android:实现多选复选框时的classCastException

时间:2013-01-02 12:03:42

标签: android android-listview

以下是我的适配器类,我正在检索手机通讯录,尝试使用复选框选择多个联系人。

 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;
            } 

            }

当我选中一个复选框并滚动复选标记重复几行时如何避免这种情况而且我在线上获得了getcgn classcast异常

 ((ViewHolder) view.getTag()).checkBox.setTag(listCont.get(position));

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

问题是,您在代码中间调用了view.setTag(contact);。因此,最终您的convertView标记为contact,而不是ViewHolder

编辑1:

替换

view.setTag(contact);

holder.checkBox.setTag(contact);

编辑2: 好的我已经在答案中添加了nizk2的评论。此外,要修复错误的复选框和图片问题,请尝试以下代码:

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;
         final Profile myContact = listCont.get(position);

        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);


              view.setTag(viewHolder);
            } 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(myContact.isSelected());
            viewHolder.checkBox
                  .setOnCheckedChangeListener(new                                                     CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                      myContact.setSelected(buttonView.isChecked());
                    }
                  });

        holder.text.setText(myContact.getName());
        myContact.getName()
        myContact.getId();

        holder.text.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                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(myContact.getPhoto() !=null && !myContact.getPhoto().equals("")){
            viewHolder.photo.setImageBitmap(myContact.getPhoto());
        }else{
            viewHolder.photo.setImageResource(R.drawable.profile);
        }

        holder.photo.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {    
                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(myContact.getNumber());
        holder.textEmailId.setText(myContact.getEmail());


        view.setClickable(true);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                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.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                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 (myContact.getAddress() != null && myContact.getAddress().length() != 0){ 
                view.setBackgroundResource(R.drawable.hasaddress); 

            } 
            return view;
            } 

            }

答案 1 :(得分:-1)

此表达式:(ViewHolder) view.getTag()

您正在将超类强制转换为子类。 getTag() returns an Object; Java中的所有的最强类。将它转换为子类(你的ViewHolder)是非常错误的(错误的我的意思是不可预测的,因为有时它可能会工作)。

Java中允许向上转换,但是向下转换会产生编译错误。可以通过添加强制转换来删除编译错误,但这可能会在运行时中断。

为什么这是错的? Look here

答案 2 :(得分:-1)

试试这个:

 CheckBox chB=(CkeckBox)((ViewHolder) view.getTag()).checkBox;
 chB.setTag(listCont.get(position));