-1

下面是我的适配器类,我在其中检索电话联系人,尝试使用复选框获取多个联系人。

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

任何帮助表示赞赏。

4

3 回答 3

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

            }
于 2013-01-02T12:37:19.600 回答
-1

这个表达式:(ViewHolder) view.getTag()

您正在将超类转换为子类。getTag() 返回一个对象;Java 中最高级的类。将其转换为子类(您的 ViewHolder)是非常非常错误的(错误的意思是不可预测的,因为有时它可能会起作用)。

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

为什么是错的?看这里

于 2013-01-02T12:15:57.790 回答
-1

尝试这个:

 CheckBox chB=(CkeckBox)((ViewHolder) view.getTag()).checkBox;
 chB.setTag(listCont.get(position));
于 2013-01-02T12:21:27.013 回答