listview中的复选框不维护用户设置的状态。

时间:2014-01-06 13:37:03

标签: android listview android-listview android-cursoradapter android-checkbox

我有一个使用listview(custon适配器)处理的类。此列表视图的行包含图像,文本视图和复选框。当用户检查滚动时,我得到滚动的编号并将其保存在数据库中。当用户可视化列表视图时,它将加载保存在数据库中并检查它的行。问题是:当用户想要取消保存数据时,他将解开文本框,但是当他取消选中并滚动列表然后返回到该位置时,会再次显示复选框。我做错了什么?

这是代码:

public class AcessoriosItemAdapter extends BaseAdapter {

ArrayList<AcessoriosItensLista> listAcessorios = new ArrayList<AcessoriosItensLista>();
Context context;    

HashSet<Integer> checkedPositions = new HashSet<Integer>();
HashSet<Integer> checked = new HashSet<Integer>();

public AcessoriosItemAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() { 
    return listAcessorios.size();
}

@Override
public Object getItem(int index) {  
    return listAcessorios.get(index);
}

@Override
public long getItemId(int index) {
    return index;
}   

public ArrayList<Integer> getChecked() {
    ArrayList<Integer> a = new ArrayList<Integer>();
    a.addAll(checked);
    return a;
}

@Override
public View getView(int index, View view, ViewGroup parent) {

    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.linha_acessorios, parent, false);             
    }       

    final AcessoriosItensLista acessorios = (AcessoriosItensLista)getItem(index);

    final ImageView imgAcessorio = (ImageView)view.findViewById(R.id.imgAcessorioLista);            

    TextView tvNome = (TextView) view.findViewById(R.id.tvNomeAcessoriosLinha);
    tvNome.setText(acessorios.getNomeAcessorio());      

    final Integer iditem = Integer.valueOf(acessorios.getId());

    boolean ch = acessorios.isChecked();

    final Integer position = Integer.valueOf(index);

    if(ch){
        checkedPositions.add(position);

        checked.add(iditem);
    }

    final CheckBox cb = (CheckBox)view.findViewById(R.id.cbListaAcessorios);        

    cb.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {           

            if(checked.contains(iditem)){                   
                for(int i = 0; i <= checked.size(); i++){
                    checked.remove(iditem);
                    checkedPositions.remove(position);
                }                   
            }

            if (((CheckBox) v).isChecked()) {                  
                checkedPositions.add(position);       
                checked.add(iditem);
                int id = context.getResources().getIdentifier("acc_gold_"+acessorios.getId(), "drawable", context.getPackageName());
                imgAcessorio.setBackgroundResource(id);                      
            }
            else if(checkedPositions.contains(position)) {
                checkedPositions.remove(position);               
                int id = context.getResources().getIdentifier("acc_"+acessorios.getId(), "drawable", context.getPackageName());
                imgAcessorio.setBackgroundResource(id);                                     

            }               

        }
    }); 


    if(checkedPositions.contains(position)){
        cb.setChecked(true);    
        int id = context.getResources().getIdentifier("acc_gold_"+acessorios.getId(), "drawable", context.getPackageName());
        imgAcessorio.setBackgroundResource(id); 

    } else {
        cb.setChecked(false);
        int id = context.getResources().getIdentifier("acc_"+acessorios.getId(), "drawable", context.getPackageName());
        imgAcessorio.setBackgroundResource(id);     

    }               

    return view;
}       


public void addDadosAcessorios(String nomeAcessorio, int id, boolean checked) {

    listAcessorios.add(new AcessoriosItensLista(nomeAcessorio, id, checked));

}

}

0 个答案:

没有答案