切换复选框编程 [英] Toggle checkbox programmatically

查看:101
本文介绍了切换复选框编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须辨认/ uncheckable项目的一个ListView。我已经设置了当前使用android.R.layout.simple_list_item_multiple_choice作为该行的ArrayAdapter,一切都显示就好了。我也能够正常获得点击这个项目。然而,该复选框在UI的不切换的时,该项目被选中。我一直在想这出一段时间,任何人都可以点我在正确的方向?我只是想知道如何强制用户界面更新,以反映该复选框的改变的状态。

I have a ListView of items that need to be checkable/uncheckable. I have set up an ArrayAdapter that is currently using android.R.layout.simple_list_item_multiple_choice as the row, and everything displays just fine. I am also able to properly get the clicks on this item. However, the Checkbox in the UI does not toggle when the item is selected. I've been trying to figure this out for a while, can anyone point me in the right direction? I just want to know how to force the UI to update to reflect the changed state of the checkbox.

如果需要,我可以提供code,但我试图寻找一些非常具体的在这里,所以我想张贴了一堆我的code不会有很大帮助。

I can provide code if needed, but I'm trying to look for something very specific here, so I figure posting a bunch of my code wouldn't be of much help.

谢谢!

推荐答案

首先通过我这样的回答:<一href="http://stackoverflow.com/questions/8060514/android-listview-with-check-boxes/8060732#8060732">Android列表视图用复选框?

First of all go through my this answer: Android listview with check boxes?

尼斯如要实现签入的ListView /选中复选框,你只需要执行下面的getView线()方法:

Nice as you want to implement checked/unchecked check boxes in ListView, you just need to implement below lines in getView() method:

 // also check this lines in the above example
ViewHolder holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(list.get(position).isSelected());

另外,还要检查getView()方法的事件对复选框的执行驻留在里面的ListView:

Also check the getView() method for the implementation of event on CheckBox residing inside the ListView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox
            .setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Model element = (Model) viewHolder.checkbox
                            .getTag();
                    element.setSelected(buttonView.isChecked());
                }
            });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    // ......  
}

这篇关于切换复选框编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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