Na  Jun Yeop Na Jun Yeop - 3 years ago 83
Android Question

Android lost of check state after scrolling

Whenever scrolling up and down, I lose check state of checkbox.

I can't solve this problem for two days......

Please help me

Here is my code.

public View getView(final int position, View view, ViewGroup parent) {

context = parent.getContext();

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


if (view == null) {

holder = new ViewHolder();

view = inflater.inflate(R.layout.custom_llistview, parent, false);

holder.img_view = (ImageView) view.findViewById(R.id.custom_imageView);
holder.txt_view = (TextView) view.findViewById(R.id.custom_textView);
holder.checkbox = (CheckBox) view.findViewById(R.id.check_box);

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

}

holder.checkbox.setChecked(check_flag_arr[position]);

holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {

check_flag_arr[position] = isChecked;
else {
check_flag_arr[position] = isChecked;
}
}
});

file_path = list_item.get(position).getFile_path();
Bitmap showImage = BitmapFactory.decodeFile(file_path);

holder.img_view.setImageBitmap(showImage);

list_contents = file_path.substring(45, 65);
holder.txt_view.setText(list_contents);

return view;
}


And detail explanation will be a great pleasure to me.

I must solve this problem.

Thanks for watching my post.

Answer Source

whenever you scroll setOnCheckedChangeListener is called

Removing setOnCheckedChangeListener and implimant setOnClickListener

holder.checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (holder.checkbox.isChecked()) {
                         check_flag_arr[position] = true;
                    } else {
                        check_flag_arr[position] = false;
                    }
                }
            });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download