简体   繁体   English

滚动后未选中ListView Adapter中的Android Checkbox

[英]Android Checkbox in ListView Adapter getting unchecked after scrolling

I have a custom ListViewAdapter with ImageView, TextView and CheckBox. 我有一个带有ImageView,TextView和CheckBox的自定义ListViewAdapter。

When I select particular checkBox and scroll down, after scrolling back, checkBox gets unchecked. 当我选择特定的复选框并向下滚动时,向后滚动后,复选框将变为未选中状态。
I tried few answers but nothing seems to work. 我尝试了几个答案,但似乎无济于事。

Here is my code: 这是我的代码:

public class ListViewAdapterDrawer extends ArrayAdapter<String> {

    ImageView imageViewSensor;
    TextView textViewSensor, textViewLogging;
    CheckBox checkBoxSensor;
    private boolean[] itemChecked;
    int count;

    public ListViewAdapterDrawer(Context context, String[] sensorArray) {
        super(context, R.layout.adapter_listview_drawer, sensorArray);
        itemChecked = new boolean[sensorArray.length];
        for (int i = 0; i < this.getCount(); i++) {
            itemChecked[i] = false;
        }
    }  

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.adapter_listview_drawer, parent, false);    
        }
        imageViewSensor = (ImageView) convertView.findViewById(R.id.imageViewSensor);
        textViewSensor = (TextView) convertView.findViewById(R.id.textViewSensor);
        checkBoxSensor = (CheckBox) convertView.findViewById(R.id.checkBoxSensor);

        checkBoxSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBoxSensor.isChecked()){
                    itemChecked[position] = true;
                }else {
                    itemChecked[position] = false;
                }
            }
        });

        checkBoxSensor.setChecked(itemChecked[position]);               
        return convertView; 
    }   
}

If you read this link regarding how ListView gets recycled, ie the recycling mechanism, you should be able to understand why your checkboxes get unselected. 如果您阅读有关如何回收ListView的链接 ,即回收机制,那么您应该能够理解为什么未选中复选框的原因。

This gives a rather detailed explanation about ListView. 给出了有关ListView的相当详细的解释。

Key Points: 关键点:

  • ListView recycles non-visible views—called “ScrapViews” in Android's source code, as you pan around. 当您平移时,ListView会回收不可见的视图,即Android源代码中称为“ ScrapViews”的视图。
  • ListView uses the view recycler to keep adding recycled views below or above the current viewport and moving active views to a recyclable pool as they move off-screen while scrolling. ListView使用视图回收器在滚动时在屏幕外移动时,在当前视口的上方或下方不断添加回收的视图,并将活动视图移动到可回收的池中。

Try to use hashmap for maintaining the state of checkbox because the listview recycles the same view when the listview is scroll 尝试使用哈希图维护复选框的状态,因为当列表视图滚动时,列表视图会回收相同的视图

  HashMap<Integer, CoreQuestion.SubSection> selectionMap = new HashMap<>();

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.adapter_listview_drawer, parent, false);    
        }
        imageViewSensor = (ImageView) convertView.findViewById(R.id.imageViewSensor);
        textViewSensor = (TextView) convertView.findViewById(R.id.textViewSensor);
        checkBoxSensor = (CheckBox) convertView.findViewById(R.id.checkBoxSensor);

        checkBoxSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBoxSensor.isChecked()){
                    itemChecked[position] = true;
                     selectionMap.put(position,"true")
                }else {
                    itemChecked[position] = false;
                     selectionMap.put(position,"false")
                }

            }
        });
       if(selectionMap.get(posotion)!=null)
       {
              checkBoxSensor.setChecked(itemChecked[position]);

       }             
        return convertView; 
    }   
}

may this help. 可能有帮助。

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

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