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

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

问题描述

使用这个扩展列表复选框,例如code 作为基准,我试图挽救和维护的复选框状态。然而,随机复选框被选中和未选中(触发我的 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;
}

我不知道是什么一块code可能是负责这一点,所以什么,包括在这里的任何建议,欢迎。我的code仅不同于原来在我出手救值。

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天全站免登陆