为什么我的ListView与CheckBox ID混淆了?

时间:2013-11-06 16:36:47

标签: android android-layout android-listview android-checkbox

我正在使用witch cell中的复选框实现ListView。但问题是,当我检查一个单元格并向下滚动列表时,它会混淆,其他单元格也会被检查。我是否想在我的getView方法中做其他事情?

这是我的CustonAdapter:

public class AcessoriosItemAdapter extends BaseAdapter {

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

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

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listAcessorios.size();
}

@Override
public Object getItem(int index) {
    // TODO Auto-generated method stub
    return listAcessorios.get(index);
}

@Override
public long getItemId(int index) {
    // TODO Auto-generated method stub
    return index;
}

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

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

    AcessoriosItensLista acessorios = (AcessoriosItensLista)getItem(index);

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

    String nomeImagem = acessorios.getNomeImagens();
    int id = context.getResources().getIdentifier(nomeImagem, "drawable", context.getPackageName());
    imgAcessorio.setBackgroundResource(id); 

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

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


    return view;
}   


public void addDadosAcessorios(String nomeAcessorio, String nomeImagens, boolean checked) {

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

}

}

1 个答案:

答案 0 :(得分:3)

此行为的原因是因为listview回收列表项视图。由于您未在getView()中重置已检查状态,因此在滚动时会保持状态并且项目将被回收。

所需要的是一种根据每个复选框的位置跟踪其状态的方法。所以你可以自信地告诉我:是否检查了k位置的复选框!

您需要跟踪已单击的复选框,以便在调用getView()时,您可以更新checkbox的状态。

1)保持list个复选框位置,并进行检查。

2)如果单击并选中了复选框,请将其位置添加到list。如果再次单击(取消选中),请将其从列表中删除。

3)使用此list并更新getView()

中复选框的选中状态

代码:

public class AcessoriosItemAdapter extends BaseAdapter {

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

    // store a list of position where checbox is selected.
    ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
    ...
    ...

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

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

        AcessoriosItensLista acessorios = (AcessoriosItensLista)getItem(index);

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

        String nomeImagem = acessorios.getNomeImagens();
        int id = context.getResources().getIdentifier(nomeImagem, "drawable", context.getPackageName());
        imgAcessorio.setBackgroundResource(id); 

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

        CheckBox cb = (CheckBox)view.findViewById(R.id.cbListaAcessorios);
        final Integer position = new Integer(index);

        // when checkbox is clicked, we add/remove its position to/from the list
        cb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {
                    // if checked, we add it to the list
                    checkedPositions.add(position);
                }
                else if(checkedPositions.contains(position)) {
                    // else if remove it from the list (if it is present)
                    checkedPositions.remove(position);
                }

            }
        });
        // set the state of the checbox based on if it is checked or not.
        cb.setChecked(checkedPositions.contains(position));

        return view;
    }   

    ...
    ...
}