保存选中按钮的状态时,返回键pressed或应用与共享preferences和onCheckChangeListener退出 [英] Saving the checked button state when back button pressed or app exited with SharedPreferences and onCheckChangeListener
问题描述
我试图做一个Android应用程序,允许用户阻止特定的时间段的应用程序。所以,我有安装的应用程序在一个开关按键片段的列表视图旁边。
我要当用户检查其或者preSS回来,退出该应用程序后,取消选中它,它留检查。
我想实现这一目标用setOnCheckedChangeListener和共享preferences;然而;我无法保存在我的BaseAdapter类按钮状态。
holder.ck1.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton compoundButton,布尔B){
如果(holder.ck1.isChecked()){
// itemChecked [位置] =真;
B =真实的;
holder.ck1.setChecked(二);
Log.i(这是,检查+位置);
共享preferences.Editor编辑= context.getShared preferences(com.ibc.android.demo.appslist.app,Context.MODE_PRIVATE).edit();
editor.putBoolean(checkBox1,B);
editor.commit(); }其他{
// itemChecked [位置] = FALSE;
B = FALSE;
holder.ck1.setChecked(二);
Log.i(这是,未选中:+位置);
共享preferences.Editor编辑= context.getShared preferences(com.ibc.android.demo.appslist.app,Context.MODE_PRIVATE).edit();
editor.putBoolean(checkBox1,B);
editor.commit(); }
} });
共享preFS = context.getShared preferences(PACKAGE_NAME,Context.MODE_PRIVATE);
holder.ck1.setChecked(共享prefs.getBoolean(checkBox1,FALSE)); //我想,也许不是假的我应该把我的方法定义的布尔B,而我不知道如何得到它。
返回convertView;}
我怎么做我修改此为达到预期的效果?
holder.ck1.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton compoundButton,布尔B)
{
共享preferences.Editor编辑= context.getShared preferences(PACKAGE_NAME,Context.MODE_PRIVATE).edit();
editor.putBoolean(pakagename,B);
editor.commit();
}});共享preFS = context.getShared preferences(PACKAGE_NAME,Context.MODE_PRIVATE);
holder.ck1.setChecked(共享prefs.getBoolean(pakagename,FALSE));
返回convertView;}
pakagename认为特定列表项的包名。否则,如果您在上面API级别11定位,您可以使用共享preferences一组来存储所有选中的包名。这是最简单的方式。否则,你可以保存在数据库中检查应用程序的列表。
I am trying to make an android app that allows users to block apps for a specific period of time. So I have a listview of installed apps in a fragment with a switch button next to it.
I want it to stay checked when the user checks its or unchecks it after they press back and exit the app.
I am trying to achieve this using setOnCheckedChangeListener and Shared Preferences; however; I am having trouble saving the button state in my BaseAdapter class.
holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (holder.ck1.isChecked()) {
//itemChecked[position] = true;
b = true;
holder.ck1.setChecked(b);
Log.i("This is", " checked: " + position);
SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
editor.putBoolean("checkBox1", b);
editor.commit();
} else {
//itemChecked[position] = false;
b= false;
holder.ck1.setChecked(b);
Log.i("This is", " not checked: " + position);
SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
editor.putBoolean("checkBox1", b);
editor.commit();
}
}
});
sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean("checkBox1",false));
// I think that maybe instead of false I should put the boolean b I defined in the method but I am not sure how to get it .
return convertView;
}
How I do I modify this to reach to desired result?
holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
SharedPreferences.Editor editor = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE).edit();
editor.putBoolean(pakagename, b);
editor.commit();
}
});
sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean(pakagename,false));
return convertView;
}
pakagename holds the package name of that particular listitem. Otherwise if you are targeting above api level 11, you can use a set in sharedpreferences to store all the checked package names. This is the easy way. Otherwise you can save the list of checked applications in a database.
这篇关于保存选中按钮的状态时,返回键pressed或应用与共享preferences和onCheckChangeListener退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!