Android,Listview多个切换问题

问题描述 投票:0回答:1

我在Custom Adapter中实现了多个开关。

CustomAdapter代码如下。

    sche_swt = (Switch)convertView.findViewById(R.id.ctschedule);
    loc_swt = (Switch)convertView.findViewById(R.id.ctlocation);

    sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
            if (compoundButton.getId() == R.id.ctschedule) {
                if (is_checked == true) {
                    contactItemList.get(position).setSchedule(true);
                } else {
                    contactItemList.get(position).setSchedule(false);
                }
            }
        }
    });

    if (contactItemList.get(position).getScheduleInt() == 1) {
        sche_swt.setChecked(true);
    }
    else
        sche_swt.setChecked(false);

    loc_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
            if (compoundButton.getId() == R.id.ctlocation) {
                if (is_checked == true) {
                    contactItemList.get(position).setLocation(true);
                }
                else {
                    contactItemList.get(position).setLocation(false);
                }
            }
        }
    });

    if (contactItemList.get(position).getLocationInt() == 1) {
        loc_swt.setChecked(true);
    }
    else
        loc_swt.setChecked(false);


    return convertView;
}

一条线看起来像

name
phone_number
switch 1 | switch 2

我存储了开关的状态

例如 :

1 line = switch(false) | switch(true)
2 line = switch(false) | switch(true)
3 line = switch(true)  | switch(false)

... 等等。

完成我的应用程序后,重新执行listview结果

1 line = switch(true) | switch(false)
2 line = switch(true) | switch(false)
3 line = switch(true) | switch(false)

怎么了??

android listview adapter uiswitch
1个回答
0
投票

问题可能出在侦听器中使用位置变量或调用setChecked方法。尝试下面,我只做了日程切换,为位置切换做同样的事情。

sche_swt = (Switch)convertView.findViewById(R.id.ctschedule);
loc_swt = (Switch)convertView.findViewById(R.id.ctlocation);

// setting tag
sche_swt.setTag(position)
loc_swt.setTag(position)

// you need to remove the listener because when you call setChecked() 
// it will invoke the listener unnecessarily and it might affect the 
// state of the view.
sche_swt.setOnCheckedChangeListener(null);
if (contactItemList.get(position).getScheduleInt() == 1) {
    sche_swt.setChecked(true);
}
else
    sche_swt.setChecked(false);

sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
        if (compoundButton.getId() == R.id.ctschedule) {
            // using the tag to get the position.
            int position = compoundButton.getTag();
            if (is_checked == true) {
                contactItemList.get(position).setSchedule(true);
            } else {
                contactItemList.get(position).setSchedule(false);
            }
        }
    }
});


return convertView;

如果您不知道标签是什么或为什么我使用它而不是位置,请阅读此https://stackoverflow.com/a/5291891/1749223。我希望以上解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.