我有一个使用custon适配器的listview。我是行的布局,我有一个文本和一个复选框。 当我加载listview时,我从数据库中获取数据,并且它有一个colunm,用于确定行是否被加载。当我加载列表时,确定,必须保持检查的行,保持检查,其他没有。问题是:当我取消勾选一行时,向下滚动列表,当我返回到开头时,我取消选中的行,再次返回检查,我该如何转售此问题:
下面的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;
}
答案 0 :(得分:2)
我的猜测是,您可能取消选中CheckBox
,但是您没有将其状态保存在任何位置,因此当该行通过滚动从屏幕上消失并再次向下滚动时,它会再次从中加载数据数据库并在其中签入。我不知道您是如何处理ArrayAdapter
扩展名的,但我建议将构造函数的ArrayList
保存为类中的实例,在取消选中时更新其中的值,然后调用{{1 }}
----编辑----
要将notifyDataSetChanged()
存储在您的课程中,您必须创建一个单独的课程(包含您正在处理的两个字段),例如:
ArrayList
因此,当您在Activity中声明自定义适配器时,您必须先声明一个带有一些初始元素(甚至为空)的ArrayList:
class MyRow {
CheckBox cb;
TextView tv;
}
然后调用适配器类的构造函数,如下所示:
ArrayList<MyRow> myList = new ArrayList<MyRow>();
MyRow row1 = new MyRow();
row1.cb.isChecked(...);
row1.tv.setText(...);
myList.add(row1);
因此,当您将它传递给适配器类的构造函数时,可以在该类中保存它的副本:
MyArrayAdapter adapter = new MyArrayAdapter(context, R.layout.your_layout, myList);
现在,您更改的任何内容(例如检查/取消选中复选框)都必须将其状态保存在myContent数组中。您可以在public class MyArrayAdapter extends ArrayAdapter {
final private ArrayList<MyRow> myContent;
...
MyArrayAdapter(Context context, int my_layout, ArrayList<MyRow> myContent_) {
...
myContent = myContent_
}
}
方法中按getItem(position)
找到该项目并进行所需的更改。之后,您只需调用getView()
方法,它就会自动显示notifyDataSetChanged();
中的更改。
答案 1 :(得分:1)
这几乎就像你的列表项目在屏幕上被重新编辑或重新创建一样,现在这里最简单明了的解决方案是在点击你的复选框时触发一个事件,所以在你的适配器中创建一个onclick事件选中或取消选中复选框时触发并更新数据源。