我正在尝试创建一个允许用户在特定时间段内阻止应用的Android应用。所以我在一个片段中有一个已安装应用的列表视图,旁边有一个切换按钮。
我希望它在用户检查时保持检查状态,或在用户按下并退出应用程序后取消选中它。
我正在尝试使用setOnCheckedChangeListener和Shared Preferences实现此目的;然而;我在BaseAdapter类中保存按钮状态时遇到问题。
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;
}
我如何修改此项以达到预期效果?
答案 0 :(得分:0)
我认为每当活动创建它时,它会自动调用onCheckedChange,并且您的SharedPreferences会默认值覆盖。所以我认为你可以使用onStop来更新SharedPreferences。
答案 1 :(得分:0)
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保存该特定listitem的包名称。否则,如果您的目标是api级别11以上,则可以使用sharedpreferences中的集合来存储所有已检查的软件包名称。这是简单的方法。否则,您可以将已检查的应用程序列表保存在数据库中。
答案 2 :(得分:0)
我已经达到了要求的功能。检查我的代码:
final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE);
switchCompat.setChecked(sharedPreferences.getBoolean("switchCondition",false));
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (switchCompat.isChecked()) {
Toast.makeText(getApplicationContext(), "Checked Condition True", Toast.LENGTH_LONG).show();
editor.putBoolean("switchCondition",switchCompat.isChecked());
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "Checked Condition False", Toast.LENGTH_LONG).show();
editor.putBoolean("switchCondition",switchCompat.isChecked());
editor.commit();
}
}
});