Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android listview中自定義適配器和復選框

android listview中自定義適配器和復選框

編輯:關於Android編程

下面的代碼中報錯了,代碼如下

@Override
p
public View getView(final int position, final View convertView, final ViewGroup parent) {
  
    final ViewHolder viewHolder;
  
    View view = convertView;
  
    if (view == null) {
  
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
  
        viewHolder = new ViewHolder();
 
 
 
        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
  
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
 
 
 
        viewHolder.checkBox.setTag(position);
  
        view.setTag(viewHolder);
 
 
 
        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
  
    } else {
  
        viewHolder = (ViewHolder) view.getTag();
  
        viewHolder.checkBox.getTag(position);
  
    }
 
 
 
    viewHolder.textTitle.setText(getItem(position).getTitle());
 
 
 
    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
  
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
        @Override
  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
            if (isChecked) {
  
                myIntegerArrayList.add(position);
  
            } else {
  
                myIntegerArrayList.remove((Object) position);
  
            }
  
        }
  
    });
 
 
 
    return view;
}
}
 
 
 
 
private static class ViewHolder {
  
    TextView textTitle;
  
    CheckBox checkBox;
}
}


該復選框本身工作正常。位置正確添加到我的ArrayList,但如果(假設我選中的項目在位置1)我把listview滾動到底部,然後再滾回去,選中取消了....

 

處理方法

 

刪掉代碼viewHolder.checkBox.setTag(position);
把 viewHolder.checkBox.getTag();放到 onCheckedChanged 中,你按照下面的代碼改動

 

@Override

public View getView(final int position, final View convertView, final ViewGroup parent) {
  
    final ViewHolder viewHolder;
  
    View view = convertView;
  
    if (view == null) {
  
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
  
        viewHolder = new ViewHolder();
 
 
 
        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
  
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
 
 
 
 
        view.setTag(viewHolder);
 
 
 
        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
  
    } else {
  
        viewHolder = (ViewHolder) view.getTag();
  
        viewHolder.checkBox.getTag(position);
  
    }
  
    viewHolder.checkBox.setTag(position);
 
 
 
    viewHolder.textTitle.setText(getItem(position).getTitle());
 
 
 
    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
  
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
        @Override
  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
            if (isChecked) {
  
                myIntegerArrayList.add(viewHolder.checkBox.getTag());
  
            } else {
  
                myIntegerArrayList.remove((Object) viewHolder.checkBox.getTag());
  
            }
  
        }
  
    });
 
 
 
    return view;
}
}


 

讓你刪掉 viewHolder.checkBox.setTag(position); 是為了 回收ListView,你需要報錯位置在tag中,並讓 onCheckedChanged 獲得到

添加viewHolder.checkBox.getTag() 也是同樣的原因

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved