滚动时不保存CheckBox状态

时间:2015-07-26 11:00:48

标签: android listview checkbox save state

我已尝试在该问题上阅读许多答案,但我无法在我的应用中使其工作.. 当我检查项目(我可以通过点击一个项目,然后单击复选框本身)时,它们会转到列表(listToRemove),一旦我点击一个按钮,该列表就被传递给另一个片段。 问题是每次我从已选中的复选框滚动时,它会再次将其状态更改为未选中状态。 我已经在SO上看到了关于这个问题的多个问题,但我无法使其发挥作用。 非常感谢帮助。

感谢。

@Override
    public void onListItemClick( ListView l, View v, int position, long id ) 
    {
        Password item = ( Password ) ( getListAdapter() ).getItem( position );
        CheckBox check = ( CheckBox ) v.findViewById( R.id.removePasswordFromHistoryCheckBox );
        if( !check.isChecked() )
        {
            if( !listToRemove.contains( item ))
            {
                listToRemove.add( item );
            }
            check.setChecked( true );
        }
        else
        {
            if( listToRemove.contains( item ))
            {
                listToRemove.remove( item );
            }
            check.setChecked( false );
        }
    }

....
....

  @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        final Password item = getItem( position );

        if (convertView == null) 
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
            holder.dateText = (TextView) convertView.findViewById( R.id.historyDateTextView ) ;
            holder.passwordText =  (TextView) convertView.findViewById( R.id.historyPasswordTextView ) ;
            holder.checkBoxToRemove = (CheckBox) convertView.findViewById( R.id.removePasswordFromHistoryCheckBox ) ;
            convertView.setTag( holder );

        } 
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if( listToRemove.contains( item ) )
        {
            holder.checkBoxToRemove.setChecked(true);
        }
        else
        {
            holder.checkBoxToRemove.setChecked(false);
        }

            holder.dateText.setText( historyList.get( position ).getDate() );
            holder.passwordText.setText( historyList.get( position ).getPasswordString() );
            holder.checkBoxToRemove.setOnCheckedChangeListener( new OnCheckedChangeListener(){
                    @Override
                    public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
                    {
                        if( isChecked )
                        {
                            if( !listToRemove.contains( item ))
                            {
                                listToRemove.add( item );
                            }
                        }
                        else
                        {
                            if( listToRemove.contains( item ))
                            {
                                listToRemove.remove( item );
                            }
                        }
                    }
                });

        return convertView;
        }      


private static class ViewHolder 
{
    TextView passwordText;
    TextView dateText;
    CheckBox checkBoxToRemove;
}

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder;
    final Password item = getItem( position );

    if (convertView == null) 
    {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
        holder.dateText = (TextView) convertView.findViewById( R.id.historyDateTextView ) ;
        holder.passwordText =  (TextView) convertView.findViewById( R.id.historyPasswordTextView ) ;
        holder.checkBoxToRemove = (CheckBox) convertView.findViewById( R.id.removePasswordFromHistoryCheckBox ) ;
        convertView.setTag( holder );

    } 
    else 
    {
        holder = (ViewHolder) convertView.getTag();
    }

    if( listToRemove.contains( item ) )
    {
        holder.checkBoxToRemove.setChecked(true);
    }
    else
    {
        holder.checkBoxToRemove.setChecked(false);
    }

        holder.dateText.setText( historyList.get( position ).getDate() );
        holder.passwordText.setText( historyList.get( position ).getPasswordString() );
        holder.checkBoxToRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    CheckBox c = (CheckBox) view;
                    if(c.isChecked())
                    {
                        if( !listToRemove.contains( item ))
                        {
                            listToRemove.add( item );
                        }
                    }
                    else
                    {
                        if( listToRemove.contains( item ))
                        {
                            listToRemove.remove( item );
                        }
                    }
                }
            });

    return convertView;
    }      

并删除onListItemClick