简体   繁体   中英

My custom listView with Checkboxes and imageViews Strange Behaviour

I had a ListView with images that are loading by using ImageLoader (framework) and checkbox for each image.I had a adapter that is extending BaseAdapter for this ListView ,when i'm checking one of the checkbox and scrolling it then i see other checkboxes which are automatically checked. Is there any way to resolve my issue. Please someone give me a clue or example.

This is because the rows are being re-used, so you need to set some attribute for each row telling it whether that row is checked or not. And then make "if and else" statement in your getView() to look if that row is checked or not, and if it is just check it, otherwise leave it unchecked.

I would suggest you use an array of booleans where you keep your checked state for every position in the list. ListView recreates list elements on scroll, so your checks could be messed up. This means that you have to check if boolean value for position of the element you're instantiating is true and then use checkBox.setChecked(true), or checkBox.setChecked(false).

Simple,

Set<String> bs = new HashSet(String);

String[] bid;

And write this in you getView method, Take it as a template. Not as a solution

public View getView(final int position, View convertView, ViewGroup arg2) {
            row = convertView;
            if (row == null)
            {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = vi.inflate(R.layout.your_listview,null);
            }

            check = (CheckBox) row.findViewById(R.id.your_checkboxid);
            check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked)
                    {
                        bs.add(bid[position]);
                    }
                    else
                    {
                        if(bs.contains(bid[position]))
                        {
                            bs.remove(bid[position]);
                        }
                    }

                }
            });

            if(bs.contains(bid[position]))
            {
                check.setChecked(true);
            }
            else
            {
                check.setChecked(false);
            }   
            return row;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM