【问题标题】:Shared Preferences between fragment and dialog片段和对话框之间的共享首选项
【发布时间】:2015-01-21 04:47:20
【问题描述】:

因此,我正在尝试保存一条我认为应该保存在共享首选项中的消息,以便我以后可以对其进行编辑并与其他片段共享。我有一个带有按钮的片段,该按钮打开一个包含编辑框的对话框,我想将写入编辑框中的任何内容保存为字符串,以便稍后在另一个活动中使用。问题是,一旦设置了消息,我就无法将其恢复,所以我根本无法编辑消息。

这是设置对话框以放入选项的按钮单击,包括单选组和编辑文本框:

case R.id.settings_my_health_alert_settings:
            //Set Up Dialog
            final Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.myhealth_alert_settings);
            dialog.setCancelable(false);
            dialog.setTitle(R.string.health_alert_dialogue_title);

            // set the custom dialog components - text, image and button
            RadioButton1 = (RadioButton)dialog.findViewById(R.id.intervalOnce);
            RadioButton2 = (RadioButton)dialog.findViewById(R.id.interval5min);
            RadioButton3 = (RadioButton)dialog.findViewById(R.id.interval10min);
            radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup);

            Button dialogButton = (Button) dialog.findViewById(R.id.ok_btn);
            Button cancelButton = (Button) dialog.findViewById(R.id.cancel);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(RadioButton1.isChecked()) {
                        AlertService.isOnce = true;
                    }
                    if(RadioButton2.isChecked()){
                        AlertService.isOnce = false;
                        intervalTime = 5;
                    }
                    if(RadioButton3.isChecked()){
                        AlertService.isOnce = false;
                        intervalTime = 10;
                    }

                    SharedPreferences.Editor mEditor = prefs.edit();
                    mEditor.putString("message", msg);
                    mEditor.apply();

                    dialog.dismiss();
                }
            });

这是 OnCreateView 的一部分,如果我想从共享首选项中获取字符串以显示每次打开对话框的时间,这似乎是放置它的地方。但我不断得到一个空指针。

SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor mEditor = prefs.edit();
                    mEditor.putString("message", msg);
                    mEditor.commit();

                    String edt = prefs.getString("message", "");
                    if (edt == null) {
                        editT = "Emergency at: " +MyHealthFragment.address;
                        editTextBox.setText(editT);
                    } else {
                        editTextBox.setText(edt);
                    }

请记住,共享首选项中的字符串也需要可供另一个片段访问。

我想不出最好的逻辑 - 把它放在 onCreateView 中?还是点击?或者我缺少的完全不同的东西,然后,如果在编辑框中没有设置任何内容,则有一个标准的备份消息......任何帮助都会很棒。我希望这是有道理的。

提前致谢。

编辑:这是我得到的 Logcat:

Caused by: java.lang.NullPointerException
        at com.nesscorporation.android.sibext.fragment.settings.dropdown.MyHealthSettingsFragment.<init>(MyHealthSettingsFragment.java:58)
        at com.nesscorporation.android.sibext.fragment.settings.dropdown.MyHealthSettingsFragment.getInstance(MyHealthSettingsFragment.java:41)

我实际上没有警报设置片段,只有对话框的 xml。

这是整个片段(我正在尝试一些东西):

public class MyHealthSettingsFragment extends BaseSettingsFragment implements View.OnClickListener {

public static MyHealthSettingsFragment getInstance(){
    return new MyHealthSettingsFragment();
}

private static final String TAG = "Koz";

private View pillRemindTitle;
private View contactsTitle;
private View alertSettings;
public static String message = "";
public static int intervalTime = 5;
public RadioButton RadioButton1;
public RadioButton RadioButton2;
public RadioButton RadioButton3;
private RadioGroup radioGroup;
private EditText editTextBox;
private String editT;
private String msg;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = View.inflate(getActivity(), R.layout.settings_my_health_fragment, null);

    pillRemindTitle = v.findViewById(R.id.settings_my_health_pills_rem);
    contactsTitle = v.findViewById(R.id.settings_my_health_contact_title);
    alertSettings = v.findViewById(R.id.settings_my_health_alert_settings);
    editTextBox = (EditText) v.findViewById(R.id.edit_message);

    pillRemindTitle.setOnClickListener(this);
    contactsTitle.setOnClickListener(this);
    alertSettings.setOnClickListener(this);

    return v;
}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.settings_my_health_pills_rem:
            startActivity(new Intent(getActivity(), PillRemindersActivity.class));
            break;
        case R.id.settings_my_health_contact_title:
            startActivity(new Intent(getActivity(), ContactsActivity.class));
            break;
        case R.id.settings_my_health_alert_settings:
            //Set Up Dialog
            final Dialog dialog = new Dialog(getActivity());

            //final String msg = message.getText().toString();
            dialog.setContentView(R.layout.myhealth_alert_settings);
            dialog.setCancelable(false);
            dialog.setTitle(R.string.health_alert_dialogue_title);
            ((TextView)dialog.findViewById(android.R.id.title)).setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"walkway_expand_bold.ttf"));

            // set the custom dialog components - text, image and button
            RadioButton1 = (RadioButton)dialog.findViewById(R.id.intervalOnce);
            RadioButton2 = (RadioButton)dialog.findViewById(R.id.interval5min);
            RadioButton3 = (RadioButton)dialog.findViewById(R.id.interval10min);
            radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup);

            Button dialogButton = (Button) dialog.findViewById(R.id.ok_btn);
            Button cancelButton = (Button) dialog.findViewById(R.id.cancel);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(RadioButton1.isChecked()) {
                        AlertService.isOnce = true;
                    }
                    if(RadioButton2.isChecked()){
                        AlertService.isOnce = false;
                        intervalTime = 5;
                    }
                    if(RadioButton3.isChecked()){
                        AlertService.isOnce = false;
                        intervalTime = 10;
                    }

                    SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor mEditor = prefs.edit();
                    mEditor.putString("message", msg);
                    mEditor.commit();

                    String edt = prefs.getString("message", "");
                    //Log.d(TAG, edt);
                    if (edt == null) {
                        //editT = "Emergency at: " +MyHealthFragment.address;
                        //editTextBox.setText(editT);
                        Log.d(TAG, "null");
                    } else {
                        Log.d(TAG, edt);
                        //editTextBox.setText(edt);
                    }

                    dialog.dismiss();
                }
            });

            cancelButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
            //SMS or MMS: radio group sms or mms (with image of map)
            break;
    }
}

我试图让任何写入对话框编辑框中的文本每次都出现在对话框编辑框中,以便可以根据用户的意愿对其进行编辑。

【问题讨论】:

  • 你的 logcat 在哪里?发布它。
  • 在 settings_my_health_alert_settings 中写入偏好设置的值?
  • 请在片段中发布更多代码,以便从您的偏好中获得价值。
  • 发布了整个片段...

标签: java android android-fragments sharedpreferences


【解决方案1】:

在您的SharedPreference 中添加消息后,您必须提交它。 在您的活动中尝试以下方式以优先增加价值:

       SharedPreferences prefs = getActivity().getSharedPreferences("my_health_settings", Activity.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = prefs.edit();
        mEditor.putString("message", msg);
        mEditor.commit();

【讨论】:

  • 是的,我试过了,但它似乎不起作用,或者改变了问题。我也再次编辑了问题。
猜你喜欢
  • 2021-02-20
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
相关资源
最近更新 更多