简体   繁体   English

ArrayAdapter更改CheckBox的值

[英]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 : 这工作正常,问题出在我重新打开对话框时,因为我将TreeSetGson一起保存在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.

相关问题 Check CheckBox从arrayadapter获取 - Check CheckBox getting from arrayadapter 排序包含更改数据的ListView ArrayAdapter - Sort ListView ArrayAdapter that Contains Changing Data GWT Java测试中的火灾事件未更改复选框的值 - Fire event in gwt java testing not changing the value of checkbox 根据更改的日期(值)动态更新复选框列表 - Dynamically update list of checkbox based on changing date (value) 为什么CheckBox Preference的摘要显示为禁用状态,为什么单击它后CheckBox的值没有变化? - Why CheckBox Preference's summary is appearing like it is disabled & why checkBox's value is not changing after clicking it? ArrayAdapter,检查是否已选中复选框,并在动画后删除行 - ArrayAdapter , check if checkbox is checked and remove the row after animation Java:ArrayAdapter获得单击的行TextView值 - Java: ArrayAdapter geting clicked row TextView value 在 android studio 中为 listView 获取 arrayList 值到 ArrayAdapter - get arrayList value into ArrayAdapter for a listView in android studio Android-ArrayAdapter:将TextView设置为SQLiteDatabase的值 - Android - ArrayAdapter: set TextView to value from SQLiteDatabase 显示arraylist的值 <hashmap> 在arrayadapter中自动完成 - Show value of arraylist<hashmap> in arrayadapter for autocomplete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM