Android,可扩展列表中随机选中/未选中的复选框 [英] Android, Checkboxes Randomly Checked/Unchecked in Expandable List

查看:19
本文介绍了Android,可扩展列表中随机选中/未选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 这个可扩展列表复选框示例代码作为基线,我正在尝试保存和维护复选框状态.但是,当我将它们滚动到视线之外,最小化它们的组,甚至最小化/最大化附近的组时,随机复选框被选中和取消选中(用新值触发我的 OnCheckedChangeListener )!

Using this Expandable List checkbox example code as an baseline, I am trying to save and maintain the checkbox state. However, random checkboxes are being checked and unchecked ( triggering my OnCheckedChangeListener with the new values ) when I scroll them out of sight, minimize their group, or even minimize/maximize a nearby group!

public Object getChild(int groupPosition, int childPosition) {
    return colors.get( groupPosition ).get( childPosition );
}

public long getChildId(int groupPosition, int childPosition) {
    return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
}

public View getChildView(final int groupPosition, final int childPosition, 
        boolean isLastChild, View convertView, ViewGroup parent) {

    View v = null;
    if( convertView != null ) {
        v = convertView;
    } else {
        v = inflater.inflate(R.layout.child_row, parent, false); 
    }

    Color c = (Color)getChild( groupPosition, childPosition );

    TextView color = (TextView)v.findViewById( R.id.childname );
    if( color != null ) {
        color.setText( c.getColor() );
    }

    TextView rgb = (TextView)v.findViewById( R.id.rgb );
    if( rgb != null ) {
        rgb.setText( c.getRgb() );
    }

    CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
    cb.setChecked( c.getState() );
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            colors.get(groupPosition).get(childPosition).setState(isChecked);
            context.setColorBool(groupPosition, childPosition, isChecked);
            Log.d("ElistCBox2", "listitem position: " +groupPosition+"/"+childPosition+" "+isChecked);
        }
    });

    return v;
}

我不知道是哪一段代码对此负责,因此欢迎对此处包含的内容提出任何建议.我的代码仅在尝试保存值时与原始代码不同.

I don't know what piece of code could be responsible for this, so any suggestions on what to include here are welcome. My code only differs from the original in my attempt to save the values.

推荐答案

这是一个非常古老的问题,但我一直在为同样的问题苦苦挣扎,所以这里是我的答案:

This is a very old question, but I struggled with the same problem so here is my answer for anybody looking:

最简单的方法是使用 CheckBox.onClickListener 而不是 onCheckedChangeListener.

The simplest way is to use CheckBox.onClickListener instead of onCheckedChangeListener.

这在重新安排您的逻辑方面只是有点烦人,但会确保当随机取消选中这些框时(例如通过扩展相邻组),事件不会触发.

This is only mildly annoying in terms of rearranging your logic, but will ensure that when the boxes are unchecked randomly (by, e.g. expanding an adjacent group) the event will not fire.

老实说,我认为这应该被视为一个错误,尽管我确信可以从 Android 源代码中解释这种行为.

Honestly I think this should be considered a bug, even though I'm sure the behaviour can be explained from the Android source.

这篇关于Android,可扩展列表中随机选中/未选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆