【问题标题】:CheckBox state not saved when scrolling滚动时未保存复选框状态
【发布时间】:2015-07-26 11:00:48
【问题描述】:

我已尝试阅读有关该问题的许多答案,但无法使其在我的应用程序中运行.. 当我检查项目时(我可以通过单击一个项目和单击复选框本身来完成),它们会转到一个列表(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;
}

【问题讨论】:

    标签: android listview checkbox save state


    【解决方案1】:

    我会这样做:

    @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

    【讨论】:

    • 谢谢。但问题是我希望能够单击该项目以及复选框以选中该复选框,并且按照您建议的方式,我必须单击该复选框本身..
    • 点击行视图时是否检查listToRemove 的大小?增加了吗?
    • 好吧,我真的没有理由检查它的大小。 List View 的数据是一个名为 HistoryList 的 arrayList。当我检查项目时,我将它们添加到名为 listToRemove 的第二个数组列表中。然后,当我单击“删除”时,listToRemove 中的对象将传递给另一个片段,并且我还将这些对象从 HistoryList 中删除。
    • 您应该考虑为此目的实现多选列表视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2018-06-30
    • 2023-03-21
    相关资源
    最近更新 更多