Android的 - ListView控件:复选框不能呆在检查 [英] Android - ListView: Checkboxes not staying checked

查看:164
本文介绍了Android的 - ListView控件:复选框不能呆在检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,大约有200个项目,我已经实现了复选框自定义ArrayAdapter。我用SparseBooleanArray存储盒的值。

I have a listview, with around 200 items, I have implemented a custom ArrayAdapter for the checkboxes. I use a SparseBooleanArray to store the value of the boxes.

所有这一切工作正常,但我似乎无法更新图形盒子的检查。如果用户点击,然后选中该复选框。但是,如果我叫setChecked在我的code,它有包装盒上的本身(所以,即使它的值是true,那么它不打勾)没有影响。

All of this works fine but I cannot seem to graphically update the checking of the boxes. If the user clicks, then the box is checked. But, if I call setChecked in my code, it has no impact on the box itself (so, even if its value is true, then it is not ticked).

我已经尝试所有箱子设置为true,没有运气,尽管它们都被检查测试,他们不表明它们。不知道什么code,你需要看到,但我在适配器的好办法打发:

I have tested by trying to set all boxes to true and no luck, even though they are all checked, they do not show that they are. Not sure what code you need to see but I have chucked in my Adapter for good measure:

class CheckBoxArrayAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener { 
private SparseBooleanArray checkedBoxes; 
Context context;

public CheckBoxArrayAdapter(Context context, int resource, List<String> objects) { 
    super(context, resource, objects); 
    checkedBoxes = new SparseBooleanArray();
    this.context = context;
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final CheckBox view = (CheckBox) super.getView(position, convertView, parent);
        //if(convertView == null) {
            //convertView = view.inflate(context, R.layout.list_item, null);
        //}
    view.setTag(position);
    view.setChecked(checkedBoxes.get(position, false));
    System.out.println(view.getTag() + ": " + view.isChecked());
    view.setOnCheckedChangeListener(this); 
    return view; 
} 

public boolean isChecked(int position) { 
    return checkedBoxes.get(position, false); 
} 
public void setChecked(int position, boolean isChecked) { 
    checkedBoxes.put(position, isChecked); 
    notifyDataSetChanged(); 
} 

public void toggle(int position) { 
    setChecked(position, !isChecked(position)); 
} 

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    checkedBoxes.put((Integer) buttonView.getTag(), buttonView.isChecked()); 
} 

}

谢谢你们!

推荐答案

好吧,看来经过文本视图是做的最好的方式,把它们作为列表项,并以此作为你的监听器:

Ok, seems that Checked Text View is the best way of doing it, use them as the list item and this as your listener:

            public void onItemClick(AdapterView<?> parent, View view, int pos,long id) 
        {
            if(!isChecked.get(pos)) {
                messageList.setItemChecked(pos, true);
                isChecked.put(pos, true);
            }else{
                messageList.setItemChecked(pos, false);
                isChecked.put(pos, false);
            }
            //sendEmail("encima+Gmail4wrdr@gmail.com", address.get(pos) + ": " +  subject.get(pos), Jsoup.parse(message.get(pos)).toString());
        }   

我用previously是由谷歌Android开发人员给出的例子,这就是为什么我认为这是司空见惯的。但是,无论是与否,这种方式是<青霉>多的简单!

这篇关于Android的 - ListView控件:复选框不能呆在检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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