【问题标题】:got setSilent error when trying to implement SharedPreferences to save state of a checkbox尝试实现 SharedPreferences 以保存复选框状态时出现 setSilent 错误
【发布时间】: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


    【解决方案1】:

    您需要创建一个在 onCheckedChangedListener 中更改的类变量,然后将其保存在 onStop() 方法的共享首选项中。

    【讨论】:

    • 它听起来很愚蠢,但我不太明白,您有示例或基本示例的链接吗?
    【解决方案2】:

    在您的活动顶部声明一个布尔变量,我们称之为检查。

    Boolean checked;
    

    最好在 onCreate 方法中初始化一个变量。

    checked = true;
    

    在您的 onCheckedChanged 方法中,根据状态将 'checked' 的值设置为 true 或 false。

    @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 方法中保存你检查的变量的值。

    @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();
    }
    

    【讨论】:

    • 非常感谢,我不知道该怎么做。我想我一定是错过了什么。因为我完成了你所要求的一切但是......我改变了 setSilent(isChecked);检查(isChecked); (我认为这是我必须做的)和检查(isChecked);仍然显示错误。我已经更新了代码。非常感谢你帮我解决这个问题
    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2021-10-21
    • 2018-05-09
    • 2014-01-19
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多