得到了当试图实现共享preferences保存复选框的状态setSilent错误 [英] got setSilent error when trying to implement SharedPreferences to save state of a checkbox
问题描述
我试图同时使用共享preferences保存复选框的状态。我花这个问题小时......我肯定是很容易解决的。我一直在这里跟随教程
I am trying to save the state of a checkbox while using SharedPreferences. I have spend hours on this problem...which I sure is easy to solve. I have been following the tutorial here
不过,我不明白什么是setSilent,我有我自己的code之内将其改为什么。 setSilent已被更改为在基于所述回答中的一个的下面code检查。我已经通过计算器很多搜查,发现相关答案负载,但没有奏效和setSilent(有时一个不同的名称)的误差总是执着。我已经张贴了我的下面code。
However, I do not understand what setSilent is and what I have to change it to within my own code. setSilent has been changed to checked in the below code based on one of the answers. I have searched through stackoverflow a lot and found loads of related answers but nothing has worked and the setSilent (sometimes a different name) error is always persistent. I have posted my code below.
SuikodenFragment.java - 错误是在这里
SuikodenFragment.java -- The error is here.
Boolean checked;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.suikoden_main_activity1, container, false);
// you can use findViewById() using the above 'view'
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.suikodenList1);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
checked = true;
return view;
}
public void onStart() {
super.onStart(); // Always call the superclass method first
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, Context.MODE_PRIVATE);
boolean isChecked = settings.getBoolean("Tick the Box", checked);
checked(isChecked);
//if(isChecked)
//checkBox.setChecked(true);
}
在code以下被放置复选框状态,这可能有助于理解我的问题。
The code below is placing the checkbox state which might help understand my problem.
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkboxTick);
checkBox.setOnCheckedChangeListener(this);
if (isChecked)
// Add the tick to the box
//Log.d(TAG, "Tick the box");
checked = true;
else
// Remove the tick in the box
//Log.d(TAG, "Untick the box");
checked = false;
}
@Override
public void onStop(){
super.onStop();
SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Tick the Box",checked).commit();
}
我真的希望有人能够帮助这一点。它可能看起来像一个愚蠢的问题,但如果它是可以解决的话,我可以完成我的应用程序的一个巨大的部分。下面的链接是最接近我自己的code ...但是当我实现他们的解决方案,checkBoxView标志了为错误<一个href=\"http://stackoverflow.com/questions/23704505/android-saving-state-of-checkbox-if-exit-app\">Android节能复选框,如果退出应用的状态。我是一个初学者,所以我很抱歉......在此先感谢!
I really hope someone can help with this. It may seem like a stupid problem but if it can be solved then I can complete a huge section of my app. The following link is the closest to my own code...but when i implement their solution, checkBoxView flags up as an error Android saving state of checkbox if exit app. I am a beginner so I apologise...thanks in advance!
推荐答案
在你活动的顶部声明一个布尔变量,让我们把它叫做检查。
At the top of your activity declare a Boolean variable, lets call it checked.
Boolean checked;
这是很好的做法来初始化变量,这样做,在你onCreate方法。
It's good practice to initialize a variable so do that in you onCreate method.
checked = true;
在您onCheckedChanged方法中设置的检查,以根据国家真或假的值。
In your onCheckedChanged method set the value of 'checked' to true or false depending on the state.
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkbox_tick);
checkBox.setOnCheckedChangeListener(this);
if (isChecked)
checked = true;
else
checked = false;
}
在您的onStop方法保存检查变量的值。
In your onStop method save the value of your checked variable.
@Override
public void onStop(){
super.onStop();
SharedPreferences settings=getActivity().getSharedPreferences(suikodenprefs,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Tick the box",checked).commit();
}
这篇关于得到了当试图实现共享preferences保存复选框的状态setSilent错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!