1

我正在女巫单元中实现一个带有复选框的 ListView。但问题是当我签入一个单元格并向下滚动列表时,它会变得混乱,而其他单元格也会被检查。我必须在我的 getView 方法中做其他事情吗?

这是我的自定义适配器:

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));

}

}

4

1 回答 1

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;
    }   

    ...
    ...
}
于 2013-11-06T16:56:04.197 回答