为什么我的ListView使用复选框标识感到困惑?我的、复选框、标识、困惑

由网友(夢醉爲紅顔)分享简介:我实现与复选框在女巫细胞一个ListView。但问题是,当我检查在一个单元格,并摇下列表,它会感到困惑和其他细胞得到检查过。难道我HAVO做别的事情在我的getView方法?这是我CustonAdapter:公共类AcessoriosItemAdapter扩展了BaseAdapter {ArrayList的< A...

我实现与复选框在女巫细胞一个ListView。但问题是,当我检查在一个单元格,并摇下列表,它会感到困惑和其他细胞得到检查过。难道我HAVO做别的事情在我的getView方法?

这是我CustonAdapter:

 公共类AcessoriosItemAdapter扩展了BaseAdapter {

ArrayList的< AcessoriosItensLista> listAcessorios =新的ArrayList< AcessoriosItensLista>();
上下文语境;

公共AcessoriosItemAdapter(上下文的背景下){
    this.context =背景;
}

@覆盖
公众诠释getCount将(){
    // TODO自动生成方法存根
    返回listAcessorios.size();
}

@覆盖
公共对象的getItem(INT指数){
    // TODO自动生成方法存根
    返回listAcessorios.get(指数);
}

@覆盖
众长getItemId(INT指数){
    // TODO自动生成方法存根
    返回指数;
}

@覆盖
公共查看getView(最终诠释索引,视图中查看,ViewGroup中父){

    如果(查看== NULL){
        LayoutInflater充气= LayoutInflater.from(parent.getContext());
        鉴于= inflater.inflate(R.layout.linha_acessorios,父母,假);
    }

    AcessoriosItensLista acessorios =(AcessoriosItensLista)的getItem(指数);

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

    串nomeImagem = acessorios.getNomeImagens();
    。INT ID = context.getResources()则getIdentifier(nomeImagem,绘制,context.getPackageName());
    imgAcessorio.setBackgroundResource(ID);

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

    复选框CB =(复选框)view.findViewById(R.id.cbListaAcessorios);


    返回查看;
}


公共无效addDadosAcessorios(字符串nomeAcessorio,字符串nomeImagens,布尔检查){

    listAcessorios.add(新AcessoriosItensLista(nomeAcessorio,nomeImagens,检查));

}
 

}

解决方案

这样做的原因行为,因为列表视图回收列表项的看法。既然你不重置的选中状态,在 getView()当您滚动和物品被回收的状态得以保持。

我们需要的是一个方法来跟踪基于其位置每个复选框的状态。所以,你可以自信地告诉:复选框在位置 K 检查或不

CheckBox和ListView的结合使用

您需要跟踪已被点击,这样,当 getView()被调用时,你可以更新状态的复选框的复选框

1)维持列表这是选中的复选框的位置。

2)如果复选框被点击并选中,添加其位置到列表。如果它被点击一次(没有),从列表中删除。

3)使用此列表,并更新 getView()

code:

 公共类AcessoriosItemAdapter扩展了BaseAdapter {

    ArrayList的< AcessoriosItensLista> listAcessorios =新的ArrayList< AcessoriosItensLista>();
    上下文语境;

    //存储的位置,其中选择checbox列表。
    ArrayList的<整数GT; checkedPositions =新的ArrayList<整数GT;();
    ...
    ...

    @覆盖
    公共查看getView(最终诠释索引,视图中查看,ViewGroup中父){

        如果(查看== NULL){
            LayoutInflater充气= LayoutInflater.from(parent.getContext());
            鉴于= inflater.inflate(R.layout.linha_acessorios,父母,假);
        }

        AcessoriosItensLista acessorios =(AcessoriosItensLista)的getItem(指数);

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

        串nomeImagem = acessorios.getNomeImagens();
        。INT ID = context.getResources()则getIdentifier(nomeImagem,绘制,context.getPackageName());
        imgAcessorio.setBackgroundResource(ID);

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

        复选框CB =(复选框)view.findViewById(R.id.cbListaAcessorios);
        最后整数位置=新的整数(指数);

        复选框,单击//时,我们添加/删除列表/它的位置,
        cb.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){

                如果(((复选框)ⅴ).isChecked()){
                    //如被选中,我们将它添加到列表
                    checkedPositions.add(位置);
                }
                否则,如果(checkedPositions.contains(位置)){
                    //否则如果从列表中删除(如果它是present)
                    checkedPositions.remove(位置);
                }

            }
        });
        //设置checbox根据国家,如果它被选中。
        cb.setChecked(checkedPositions.contains(位置));

        返回查看;
    }

    ...
    ...
}
 

I'm implementing a ListView with checkbox in witch cell. But the problem is when I check in one cell and roll down the list, it get confused and others cells get checked too. Do I havo to do something else in my getView method?

This is my 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));

}

}

解决方案

The reason for this behaviour is because listview recycles the list item views. Since you do not reset the checked state in the getView() the state is maintained when you scroll and items gets recycled.

What is needed is a way to keep track of the state of each checkbox based on its position. So you could confidently tell: checkbox at position k is checked or not!

You need to keep track of the checkboxes which have been clicked so that when getView() is called, you can update the state of the checkbox.

1) Maintain a list of checkbox positions which are checked.

2) If checkbox is clicked and checked, add its position to the list. If it is clicked again (unchecked), remove it from the list.

3) Use this list and update the checked state of the checkbox in the getView()

Code:

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

    ...
    ...
}

阅读全文

相关推荐

最新文章