1

I have a strange problem with my checkbox. In my adapter I have a TextView, an ImageView and a CheckBox. I had implement a button for select all/deselect all and everything works fine. The problem is that when I select one checkbox, if I scroll the list there are and other checkbox selected(the distance between them is 10) and I don't understand why. Can anyone help me?

Here is my adapter :

public class LazyAdapter extends BaseAdapter {

        private Activity activity;
        private String[] data;
        private String[] nume;
        private LayoutInflater inflater = null;
        public ImageLoader imageLoader;

        public LazyAdapter(Activity a, String[] d, String[] f) {
            activity = a;
            data = d;
            nume = f;
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader = new ImageLoader(activity.getApplicationContext(),"Folder2");
        }

        public int getCount() {
            return data.length;
        }

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

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

        public class ViewHolder {
            public TextView text;
            public ImageView image;
            public CheckBox ck;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            ViewHolder holder;
            if (convertView == null) {
                vi = inflater.inflate(R.layout.item, null);
                holder = new ViewHolder();
                holder.text = (TextView) vi.findViewById(R.id.text);
                holder.image = (ImageView) vi.findViewById(R.id.image);
                holder.ck = (CheckBox) vi.findViewById(R.id.chkbox);
                if (flag)
                    holder.ck.setChecked(true);
                else
                    holder.ck.setChecked(false);

                vi.setTag(holder);
            } else
                holder = (ViewHolder) vi.getTag();

            holder.text.setText(nume[position]);
            holder.image.setTag(data[position]);
            imageLoader.DisplayImage(data[position], activity, holder.image);
            return vi;
        }

    }
2
  • flag is a boolean. I use it for selectall/deselectall button : selectall.setOnClickListener(new OnClickListener() { public void onClick(View v) { String txt = textbutton(); if (txt.equals("Select All")) { flag = true; selectall.setText("Deselect All"); } else { flag = false; selectall.setText("Select All"); } getData(); } }); Commented Jul 27, 2011 at 14:52
  • Ok, you've accepted incorrect answer :) Commented Jul 27, 2011 at 15:16

2 Answers 2

2

Views in list are recycled. Your data list may has 100 rows, but ListView has only 10 (these visible) rows. When you scroll, rows are recycled. The last row in ListView goes to the first position or the other way. You have to change the state of these rows. You need to keep checkbox state in array, not in views.

Add

        final List<Boolean> isCheckedList;

initialize it in constructor:

        isCheckedList = new ArrayList<Boolean>();
        for(int i=0;i<myDataLength;i++){
            isCheckedList.add(Boolean.FALSE);
        }

and modify getView:

        holder.ck.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                isCheckedList.set(position, holder.ck.isChecked());
            }
        });

in place where you fill rows:

        holder.ck.setChecked(isCheckedList.get(position));

To change state of all checkboxes use:

        Collections.fill(isCheckedList, Boolean.FALSE);
        notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

2 Comments

I think is ok what you wrote but I get nullpointerexception at line holder.ck.setChecked(isCheckedList.get(position)); Where is the problem?
You're right, I've corrected the code. Now it works, just tested it :)
1

You need to move this code:

    if (flag)
         holder.ck.setChecked(true);
    else
         holder.ck.setChecked(false);

out of your if{} block.

1 Comment

This works only partialy because once I select something from the top of the list and I scroll to the bottom of the list when I get back at the top of the list the item is not selected anymore.It gets unchecked somehow:)Thanks;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.