使用共享首选项来检索数据

时间:2016-07-28 07:18:05

标签: java android android-edittext sharedpreferences

我有一个年龄输入编辑文本,我希望使用共享首选项(用于首次输入后)将用户以前的输入放在那里。我写了下面的代码,但强行关闭。有什么问题?

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//setting text for editText
age_editText.setText(my_prefs.getInt("age_value", 0) + ""); //when i delete this part, it doesn't force close

btn_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                try {
                    Int age_value = Integer.parseInt(age_editText.getText().toString());
                    //shared preferences
                    sharedPreferences my_prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = my_prefs.edit();
                    editor.putInt("age_value", age_value);
                    editor_bmi.commit();

                } catch (Exception e) {
                    Toast.makeText(getContext(), getString(R.string.please_fill_all_inputs), Toast.LENGTH_LONG).show();
                }
        }
    });

3 个答案:

答案 0 :(得分:1)

您需要使用getSharedPreferences()代替getPreferences(),如下所示。

SharedPreferences sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);

注意: getPreferences()getSharedPreferences()是返回不同Preference对象的两种不同方法。

答案 1 :(得分:0)

更改

sharedPreferences my_prefs = getActivity().getPreferences(Context.MODE_PRIVATE);

sharedPreferences my_prefs = getActivity().getSharedPreferences("my_pref", Context.MODE_PRIVATE);

并在上面写下

age_editText.setText(my_prefs.getInt("age_value", 0) + "");

正确的方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    sharedPreferences my_prefs getActivity().getSharedPreferences("my_pref", Context.MODE_PRIVATE);
    age_editText.setText(my_prefs.getInt("age_value", 0) + ""); //when i delete this part, it doesn't force close

    btn_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                Int age_value = Integer.parseInt(age_editText.getText().toString());

                SharedPreferences.Editor editor = my_prefs.edit();
                editor.putInt("age_value", age_value);
                editor_bmi.commit();

            } catch (Exception e) {
                Toast.makeText(getContext(), getString(R.string.please_fill_all_inputs), Toast.LENGTH_LONG).show();
            }
        }
    });
}

答案 2 :(得分:0)

您可以使用以下方法将年龄保存到SharedPreferences:

public void saveAge(int age_value) {
        SharedPreferences pre = getSharedPreferences("my_age", MODE_PRIVATE);
        SharedPreferences.Editor editor = pre.edit();
        editor.putInt("age_value", age_value);
        editor.commit();
    }

当你想要了解你的年龄时,请使用:

public int readAge() {
        SharedPreferences pre = getSharedPreferences("my_age", MODE_PRIVATE);
        int age = pre.getInt("age_value", 0);
        return age;
    }
相关问题