简体   繁体   English

我的自定义listView与复选框和imageViews奇怪的行为

[英]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. 我有一个包含使用ImageLoader (框架)加载的图像的ListView和每个图像的复选框。我有一个适配器,它为此ListView扩展了BaseAdapter ,当我检查其中一个复选框并滚动它时,我看到了其他复选框会自动检查。 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. 然后在您的getView()执行“ if and else”语句,以查看该行是否被选中,以及是否仅对其进行检查,否则保持未选中状态。

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. ListView在滚动时重新创建列表元素,因此您的检查可能被弄乱了。 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). 这意味着您必须检查要实例化的元素位置的布尔值是否为true,然后使用checkBox.setChecked(true)或checkBox.setChecked(false)。

Simple, 简单,

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

String[] bid;

And write this in you getView method, Take it as a template. 并将其写在您的getView方法中,将其作为模板。 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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