[英]ArrayAdapter is changing the value of the CheckBox
I don't know what's happening, I've a list of objects and depending of its state it's checked or not ( CheckBox
). 我不知道发生了什么,我有一个对象列表,根据它是否被检查( CheckBox
)的状态而定。 I've tried too many ways, because first of all I wanted to return an ArrayList<myObject>
with the state updated, but I don't know why the objects were duplicating... and I ended up creating a TreeSet
(don't know if it's the better way but at least the objects now doesn't repeat)... well the thing is that on my Adapter
I have this : 我已经尝试了太多方法,因为首先我想返回状态已更新的ArrayList<myObject>
,但是我不知道为什么对象会重复...而最终我创建了TreeSet
(不要不知道这是否是更好的方法,但是至少现在不重复对象了。。。好吧,这是在我的Adapter
我有这个:
final Sessio sessio = getItem(position);
ALAdapter.add(sessio);
vh.tvSessioName.setText(sessio.getName());
vh.cbAssist.setChecked(sessio.isState());
vh.cbAssist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(mContext, "chaning", Toast.LENGTH_SHORT).show();
sessio.setState(isChecked);
}
});
AlAdapter is private TreeSet<Sessio> ALAdapter;
AlAdapter是private TreeSet<Sessio> ALAdapter;
this is working fine, the problem is at the time I re-open the dialog because I save the TreeSet
on a json
with Gson
as follows : 这工作正常,问题出在我重新打开对话框时,因为我将TreeSet
与Gson
一起保存在json
上,如下所示:
Gson gson = new Gson();
String json = mPrefs.getString("ArrayListSesio", "");
Type type = new TypeToken <TreeSet<Sessio>> () {}.getType();
ArrayList<Sessio> obj = gson.fromJson(json,type);
return obj == null ? AlSessio : obj;
This is also working fine... I think the problem is on the Adapter
because if I uncked some of the CheckBox
and I change the state of the Sessio
when I re-open the Dialog
it shows the Toast
like 15 times... and everytime I scroll up/down the state of the CheckBox
changes.... 这也可以正常工作...我认为问题出在Adapter
上,因为如果我打开一些CheckBox
并在重新打开Dialog
时更改了Sessio
的状态,它会显示Toast
Sessio
15次...并且每次我上下滚动CheckBox
的状态都会改变。
What I'm doing wrong? 我做错了什么? Is there any other way to instead of save it to a TreeSet
save it to an ArrayList
? 还有其他方法可以代替将其保存到TreeSet
并将其保存到ArrayList
吗?
Problem here is that views are reused, so before using setChecked
method you should do this: 这里的问题是视图被重用,因此在使用setChecked
方法之前,您应该这样做:
vh.cbAssist.setOnCheckedChangeListener(null);
vh.cbAssist.setChecked(sessio.isState());
vh.cbAssist.setOnCheckedChangeListener(/* your listener here */);
Otherwise using setChecked
will trigger listener. 否则,使用setChecked
将触发侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.