0

私のアプリケーションはListView(カスタムアダプター)を使用してすべての画像を表示しcheckbox(CB)、選択した画像にマークを付けます。

3 つのイメージを選択したら、イメージを追加しないようにすべての CB を無効にする必要があります。

これが私のコードです:

MainActivity: OnCheckedChangedListener を実装します

@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
     if (isChecked){
          checkbox = (CheckBox) arg0.getTag(R.id.check);
          checkbox.setEnabled(false);
      } 
}

MyAdapter: ArrayAdapter(Model) を拡張します

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);

        viewHolder = new ViewHolder();

        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.text2 = (TextView) convertView.findViewById(R.id.label2);
        viewHolder.img  = (ImageView)convertView.findViewById(R.id.img);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);

        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                counter++;
                Log.d("mer", counter + " counter");

            }
        });

        convertView.setTag(viewHolder);

        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.label2, viewHolder.text2);
        convertView.setTag(R.id.img, viewHolder.img);
        convertView.setTag(R.id.check, viewHolder.checkbox);

        } else {

        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.text.setText(list.get(position).getName());
    viewHolder.text2.setText(list2.get(position).getName());
    Bitmap bmImg = BitmapFactory.decodeFile(list2.get(position).getName());
    Bitmap bmThumbnail = ThumbnailUtils.extractThumbnail(bmImg,75, 75);
    viewHolder.img.setImageBitmap(bmImg);
    viewHolder.checkbox.setChecked(list.get(position).isSelected());

    return convertView;
}
4

1 に答える 1

0

array adapter追加条件で、次のようにリストのサイズを確認します:

        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   if(list.size() < 3)
                      {
                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                        counter++;
                        Log.d("mer", counter + " counter");
                      }
                  else
                      {
                        buttonView.setChecked(false);   
                      }
                }
            });
于 2013-08-30T08:26:41.490 に答える