【发布时间】:2014-01-13 16:44:37
【问题描述】:
我有一个带有自定义适配器的列表视图。在行的布局中,我有一个文本和一个复选框。 当我加载列表视图时,我从数据库中获取数据,它有一个列来确定该行是否被检查。当我加载列表时,它可以,必须保持选中的行,保持选中状态,其他行没有。问题是:当我取消选中一行并将列表向下和向上滚动时,当我返回开始时,我未选中的行再次返回选中,我该如何转售这个问题:
getView() 代码如下:
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);
final CheckBox cb = (CheckBox)view.findViewById(R.id.cbListaAcessorios);
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){
if(!checked.contains(iditem)){
checkedPositions.add(position);
checked.add(iditem);
}
}
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(checked.contains(iditem)){
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);
checked.remove(iditem);
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;
}
【问题讨论】:
-
使用自定义listView来设计你想要的布局
-
我正在使用 CustonAdapter。
-
@Roland 这是因为 listvire 回收视图。检查这个stackoverflow.com/questions/18162931/…
标签: android listview android-listview android-adapter android-checkbox