为什么在我向上滚动网格视图后取消选中该复选框?

时间:2013-07-08 07:43:52

标签: android gridview checkbox

我在其中创建了一个带有图像视图和复选框的网格视图,并在该网格视图中对我的复选框和图像视图进行了充气。如果我选中了我的复选框,然后向下/向上滚动网格视图,则检查到网格的复选框已取消选中。 我的代码有什么问题

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

2 个答案:

答案 0 :(得分:0)

holder.checkbox.setId(position);之后

你应该添加:

holder.checkbox.setChecked(thumbnailsselection[id]);

答案 1 :(得分:0)

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

您选中的更改侦听器应如下所示:

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
    }

}

}

DON“T使用onCheckedChanged,改用onClick,这对我有用。