首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从DialogFragment读取/写入首选项?

如何从DialogFragment读取/写入首选项?
EN

Stack Overflow用户
提问于 2013-01-27 01:35:50
回答 1查看 4.5K关注 0票数 9

我想从DialogFragment中的首选项文件中读取。如果我这样做:

代码语言:javascript
复制
prefs = getSharedPreferences("numberPicker.preferences", 0);

然后我得到一个编译时错误,因为getSharedReference是一个ContextWrapper方法,而DialogFragment不是ContextWrapper (顺便说一句,我使用android.support.v4.app.DialogFragment是为了向后兼容)。

或者,作为一种“变通办法”,我使用在类InitSpel中创建的SharedPreferences对象prefs (它是一个FragmentActivity,因此是一个ContextWrapper),那么我不会得到错误(无论是在编译时还是在运行时),但是这些值不会被存储(下次我启动应用程序时,baan1和baan2的值仍然是0)。

如何解决?

代码语言:javascript
复制
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();
    }   
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-27 01:42:56

使用getActivity(),因为Activity扩展了Context,而Context有一个getSharedPreferences()方法。

代码语言:javascript
复制
prefs = getActivity().getSharedPreferences("numberPicker.preferences", 0);

我还建议保留对Editor对象的引用,以确保正确保存。

代码语言:javascript
复制
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("BAAN_01", baan1);
editor.putInt("BAAN_02", baan2);

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

或链式:

代码语言:javascript
复制
prefs.edit()
  .putInt("BAAN_01", baan1)
  .putInt("BAAN_02", baan2)
  .commit();
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14539615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档