我正在女巫单元中实现带有复选框的 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));
}
}
这种行为的原因是因为listview
回收列表项视图。由于您没有重置中的选中状态getView()
当您滚动并且项目被回收时,状态会被维持。
需要的是一种根据每个复选框的位置来跟踪其状态的方法。所以你可以自信地告诉:复选框位于位置k
是检查还是不检查!
您需要跟踪已单击的复选框,以便何时getView()
被调用时,您可以更新状态checkbox
.
1)维持一个list
已选中的复选框位置。
2) 如果单击并选中复选框,则将其位置添加到list
。如果再次单击(未选中),请将其从列表中删除。
3)使用这个list
并更新复选框的选中状态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;
}
...
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)