Java Android-ListView:复选框未保持选中状态

Java Android-ListView:复选框未保持选中状态,java,android,listview,checkbox,Java,Android,Listview,Checkbox,我有一个列表视图,大约有200个项目,我为复选框实现了一个定制的ArrayAdapter。我使用SparseBooleanArray来存储框的值 所有这些都很好,但我似乎无法以图形方式更新复选框。如果用户单击,则选中该框。但是,如果在代码中调用setChecked,它对框本身没有影响(因此,即使其值为true,也不会勾选) 我通过尝试将所有框设置为true和no-luck来进行测试,即使它们都被选中,也不会显示它们是。不确定您需要查看哪些代码,但我已经插入了适配器,以便更好地测量: class

我有一个列表视图,大约有200个项目,我为复选框实现了一个定制的ArrayAdapter。我使用SparseBooleanArray来存储框的值

所有这些都很好,但我似乎无法以图形方式更新复选框。如果用户单击,则选中该框。但是,如果在代码中调用setChecked,它对框本身没有影响(因此,即使其值为true,也不会勾选)

我通过尝试将所有框设置为true和no-luck来进行测试,即使它们都被选中,也不会显示它们是。不确定您需要查看哪些代码,但我已经插入了适配器,以便更好地测量:

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()); 
} 
类CheckBoxArrayAdapter扩展ArrayAdapter实现CompoundButton.OnCheckedChangeListener{
专用SparseBooleanArray复选框;
语境;
公共CheckBoxArrayAdapter(上下文上下文、int资源、列表对象){
超级(上下文、资源、对象);
checkedboxs=new SparseBooleanArray();
this.context=上下文;
} 
@凌驾
公共视图getView(int位置、视图转换视图、视图组父级){
最终复选框视图=(复选框)super.getView(位置、转换视图、父级);
//if(convertView==null){
//convertView=view.inflate(上下文,R.layout.list_项,空);
//}
视图.设置标签(位置);
view.setChecked(checkedboxs.get(position,false));
System.out.println(view.getTag()+”:“+view.isChecked());
view.setOnCheckedChangeListener(这个);
返回视图;
} 
公共布尔值已检查(int位置){
返回复选框。获取(位置,false);
} 
public void setChecked(int位置,布尔值已检查){
复选框。放置(位置,已检查);
notifyDataSetChanged();
} 
公共无效切换(int位置){
设置已检查(位置,!已检查(位置));
} 
检查更改后的公共无效(复合按钮视图,布尔值已检查){
checkedboxs.put((整数)buttonView.getTag(),buttonView.isChecked());
} 
}


谢谢大家

好的,看来选中文本视图是最好的方法,将它们用作列表项,并将其用作您的侦听器:

            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());
        }   
public void-onItemClick(AdapterView父项、视图、int-pos、长id)
{
如果(!isChecked.get(pos)){
messageList.setItemChecked(pos,true);
isChecked.put(pos,true);
}否则{
messageList.setItemChecked(pos,false);
已检查。放置(位置,错误);
}
//发送电子邮件(“encima+Gmail4wrdr@gmail.com“,address.get(pos)+”:“+subject.get(pos),Jsoup.parse(message.get(pos)).toString());
}   

我之前使用的例子是由一个谷歌安卓开发人员给出的,这就是为什么我认为这很平常的原因。但是,不管是不是,这种方法要简单得多

可能不值得一提,但即使在XML中设置复选框为启用,也没有显示任何内容,当然我遗漏了一些次要的内容,但我看不到它!出于好奇,为什么不在ListView中使用内置的对multi-select的支持呢?我已经将android:choiceMode添加为多选模式,但似乎普遍的共识是使用自定义适配器。大多数安卓应用程序似乎都包含了复选框。普遍的共识在哪里?在什么情况下?我不认为这些是相互排斥的。关键是你不需要自己跟踪检查状态,你只需要说通过