0

我创建了一个网格视图,其中包含图像视图和复选框,并在该网格视图中扩展了我的复选框和图像视图。如果我检查了我的复选框,然后向下/向上滚动网格视图,则已选中网格的复选框已被取消选中。 我的代码有什么问题

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    public ImageAdapter(Context c, boolean checkAll) {
        context = c;
        isCheckAll = checkAll;
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.a_6th_main_checkbox, null);


            holder.imageview = (ImageView) convertView.findViewById(R.id.img_thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbo_CheckImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();

        }


        holder.checkbox.setId(position);
        holder.imageview.setId(position);

        holder.checkbox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                CheckBox cb = (CheckBox) v;
                int id = cb.getId();

                if(cb.isChecked()){
                    cb.setChecked(true);
                    thumbnailsselection[id] = true;

                }
                else if (!cb.isChecked()){
                    cb.setChecked(false);
                    thumbnailsselection[id] = false;
                }

            }
        });
        final int id = holder.checkbox.getId();
        holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                CheckBox check = (CheckBox) buttonView;

                if(buttonView.isChecked() == isChecked){

                    thumbnailsselection[id] = true;

                }
                else {
                    thumbnailsselection[id] = false;
                }
            }
        });
        holder.imageview.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);

            }
        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        if (!isCheckAll){
            holder.checkbox.setChecked(false);
        }else{
            holder.checkbox.setChecked(true);

        }

        holder.id = position;
        return convertView;
    }
}
class ViewHolder {
    CheckBox checkbox, cbo_SelectAll;
    ImageView imageview;
    int id;
}
}
4

2 回答 2

0

仅当 convertview 为空时,您才设置复选框的标记。这只发生在记录的第一个屏幕上。当用户向下滚动时,之前的转换视图被回收。因此,您的复选框将较旧的数据项作为其标签。

您检查的更改侦听器应如下所示:

new CompoundButton.OnCheckedChangeListener()
{
   @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
    {
    Person element = (Person) viewHolder.checkBox.getTag();

    data[position].setCheck(isChecked);

    if(isChecked)
    {
        // do your stuff
    }
    else
    {
        //to-do
    }

}

}

不要使用 onCheckedChanged,而是使用 onClick,这对我有用。

于 2013-07-08T08:11:00.453 回答
0

holder.checkbox.setId(position);

您应该添加:

holder.checkbox.setChecked(thumbnailsselection[id]);
于 2013-07-08T07:48:27.227 回答