国家不切换按钮更改 [英] State not change of toggle button

查看:194
本文介绍了国家不切换按钮更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个切换按钮列表视图当我toggal点击链接,然后向下滚动,则该按钮的状态更改为关闭状态
请不要重播

I have listview with toggle button when i click on toggal button and scroll down then state of the button will change to Off state Please do the replay

推荐答案

使用共享preference 保存每个状态切换按钮,从加载状态共享preference

use sharedpreference to save the state of each toggle button and load the state from the sharedpreference

就像你可以共享preference定义切换的状态(有唯一ID)

Like you can define a state of your toggle (which has unique id) in sharedpreference

下面的定义适配器定义的共享preference值。

Here in Custom adaptor defined shared preference value.

final ToggleButton tgl=(ToggleButton)row.findViewById(R.id.tglalertstatus);

            tgl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked)
                    {
                        SharedPreferences contact = context.getSharedPreferences(
                                "contact", 1);

                        editor = contact.edit();

                        editor.putInt("toggle"+tgl.getContentDescription().toString(), 1); // i set the content description for each toggle a unique string so it will work as a key for shared preference. 

                        editor.commit();
                    }
                    else
                    {
                        SharedPreferences contact = context.getSharedPreferences(
                                "contact", 1);

                        editor = contact.edit();

                        editor.putInt("toggle"+tgl.getContentDescription().toString(), 0);//i set the content description for each toggle a unique string so it will work as a key for shared preference.

                        editor.commit();
                    }

                }
            });

希望这会有所帮助。

hope this will help.

这篇关于国家不切换按钮更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆