-1

以下は、電話の連絡先を取得し、チェックボックスを使用して複数の連絡先を選択しようとしている私のAdapterクラスです。

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

            }

1つのチェックボックスをオンにして、チェックマークをスクロールすると、それを回避する方法が数行繰り返され、行でクラスキャスト例外が発生します。

 ((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 に答える