【发布时间】:2014-06-14 18:07:13
【问题描述】:
我正在尝试在使用 SharedPreferences 时保存复选框的状态。我在这个问题上花了几个小时......我肯定很容易解决。我一直在关注教程here
但是,我不明白 setSilent 是什么以及我必须在自己的代码中将其更改为什么。 setSilent 已根据其中一个答案更改为在以下代码中进行检查。我已经通过stackoverflow进行了很多搜索,发现了大量相关答案,但没有任何效果,并且setSilent(有时是不同的名称)错误总是持续存在的。我已经在下面发布了我的代码。
SuikodenFragment.java -- 错误就在这里。
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);
}
下面的代码放置了复选框状态,这可能有助于理解我的问题。
@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();
}
我真的希望有人可以帮助解决这个问题。这似乎是一个愚蠢的问题,但如果可以解决,那么我可以完成我的应用程序的很大一部分。以下链接最接近我自己的代码......但是当我实施他们的解决方案时,checkBoxView 标记为错误Android saving state of checkbox if exit app。我是初学者,所以我很抱歉......提前谢谢!
【问题讨论】:
标签: android checkbox sharedpreferences