【问题标题】:Android Checkbox in ListView Adapter getting unchecked after scrolling滚动后未选中 ListView 适配器中的 Android 复选框
【发布时间】:2015-08-23 15:10:30
【问题描述】:

我有一个带有 ImageView、TextView 和 CheckBox 的自定义 ListViewAdapter。

当我选择特定的复选框并向下滚动时,向后滚动后,复选框将被取消选中。
我尝试了几个答案,但似乎没有任何效果。

这是我的代码:

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; 
    }   
}

【问题讨论】:

    标签: android listview checkbox android-listview android-custom-view


    【解决方案1】:

    如果您阅读此link 关于 ListView 如何被回收,即回收机制,您应该能够理解为什么您的复选框未被选中。

    This对ListView做了比较详细的解释。

    要点:

    • ListView 在您平移时回收不可见的视图(在 Android 的源代码中称为“ScrapViews”)。
    • ListView 使用视图回收器不断在当前视口下方或上方添加回收视图,并在活动视图在滚动时移出屏幕时将它们移动到可回收池中。

    【讨论】:

      【解决方案2】:

      尝试使用hashmap来维护checkbox的状态,因为listview在滚动时会回收同一个view

        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; 
          }   
      }
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-28
        • 2011-02-04
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-05-10
        • 1970-01-01
        • 2014-03-20
        相关资源
        最近更新 更多