简体   繁体   English

如何从 DialogFragment 读/写首选项?

[英]How to read/write preferences from a DialogFragment?

I want to read from a preferences file in a DialogFragment.我想从 DialogFragment 中的首选项文件中读取。 If I do this:如果我这样做:

prefs = getSharedPreferences("numberPicker.preferences", 0);

then I get a compile time error, because getSharedReference is a ContextWrapper method but DialogFragment isn't a ContextWrapper (I use android.support.v4.app.DialogFragment for backwards compatibility by the way).然后我得到一个编译时错误,因为 getSharedReference 是一个 ContextWrapper 方法但 DialogFragment 不是一个 ContextWrapper (顺便说一下,我使用 android.support.v4.app.DialogFragment 来实现向后兼容性)。

If alternatively, as a 'workaround', I use a SharedPreferences object prefs created in a class InitSpel (which is a FragmentActivity and therefore IS a ContextWrapper) then I get no error (nor at compile time nor at runtime) however the values are not stored (next time I start the app the values baan1 and baan2 are still 0).如果或者,作为“解决方法”,我使用在类 InitSpel(它是 FragmentActivity,因此是 ContextWrapper)中创建的 SharedPreferences 对象首选项,那么我没有错误(在编译时或运行时)但值不是存储(下次启动应用程序时,值 baan1 和 baan2 仍为 0)。

How to solve?怎么解决?

package mypackage;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class VraagBanenDialogFragment extends DialogFragment {

    private View v;
    private EditText editText1; 
    private EditText editText2; 
    //private ArrayList<Baan> baanNummers;
    private int[] oldBanen;
    private int[] currentBanen;
    private SharedPreferences prefs;

    /*  public VraagBanenDialogFragment(ArrayList<Baan> baanNummers) {
        this.baanNummers = baanNummers;
    }
*/
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Restore preferences
        //prefs = InitSpel.prefs;
        prefs = getSharedPreferences("numberPicker.preferences", 0);
        int baan1 = prefs.getInt( "BAAN_01", 0 );
        int baan2 = prefs.getInt( "BAAN_02", 0 );
        //oldBanen = new int[InitSpel.aantalParallel];
        oldBanen = new int[2]; 
        oldBanen[0] = baan1;
        oldBanen[1] = baan2;

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        v = inflater.inflate(R.layout.vraag_banen, null);
        // velden vullen met opgeslagen waarden
        editText1 = (EditText) v.findViewById(R.id.editText1); 
        editText2 = (EditText) v.findViewById(R.id.editText2); 
        editText1.setText(String.valueOf(baan1));
        editText2.setText(String.valueOf(baan2));
        //editText1.setText(String.valueOf(baanNummers.get(0).getBaanNummer()));
        //editText2.setText(String.valueOf(baanNummers.get(1).getBaanNummer()));
        builder.setView(v)
        // Add action buttons
        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

               int baan1 = Integer.valueOf(editText1.getText().toString());
               int baan2 = Integer.valueOf(editText2.getText().toString());
               InitSpel.setBaanNummer(0, baan1);
               InitSpel.setBaanNummer(1, baan2);

               // en banen nog bij de preferences op schijf opslaan...
               // We need an Editor object (prefs.edit()) to make preference changes.
               // All objects are from android.context.Context
               prefs.edit().putInt("BAAN_01", baan1);
               prefs.edit().putInt("BAAN_02", baan2);

               // Commit the edits!
               prefs.edit().commit();
            }
        })
        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
               // TODO cancel;
            }
        });      
        return builder.create();
    }   
 }

Use getActivity() since Activity extends Context , and Context has a getSharedPreferences() method.使用getActivity()因为Activity扩展了Context ,并且Context有一个getSharedPreferences()方法。

prefs = getActivity().getSharedPreferences("numberPicker.preferences", 0);

I'd also recommend keeping a reference to the Editor object to ensure proper saving.我还建议保留对 Editor 对象的引用以确保正确保存。

SharedPreferences.Editor editor = prefs.edit();
editor.putInt("BAAN_01", baan1);
editor.putInt("BAAN_02", baan2);

// Commit the edits!
editor.commit();

or chain:或链:

prefs.edit()
  .putInt("BAAN_01", baan1)
  .putInt("BAAN_02", baan2)
  .commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM