使用getter / setter方法的Android共享首选项无法显示

时间:2014-06-27 09:31:34

标签: java android sharedpreferences getter-setter findall

我的Android应用无法显示我在其他课程中设置的共享偏好设置。 我在代码中遗漏了什么吗?我保存得很好但是它们没有显示在我的主Activity中。我看到的只是运行应用程序时的默认文本。我尽力而为无济于事。我无法弄清楚问题出在哪里。以下是我的代码文件:

1。 MnsEditor.java

    public class MnsEditor extends Activity {

    private MsItem mnses;
    private Button saveBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mns_editor);

        Intent intent = this.getIntent();
        mnses = new MsItem();
        mnses.setStartKey(intent.getStringExtra("startKey"));
        mnses.setStartDate(intent.getStringExtra("StartDate"));

        mnses.setShiftKey(intent.getStringExtra("ShiftKey"));
        mnses.setShift(intent.getStringExtra("Shift"));


        mnses.setBKey(intent.getStringExtra("BKey"));
        mnses.setB(intent.getStringExtra("B"));

        mnses.setGKey(intent.getStringExtra("GKey"));
        mnses.setG(intent.getStringExtra("G"));

        mnses.setNxtKey(intent.getStringExtra("NxtKey"));
        mnses.setNxt(intent.getStringExtra("Nxt"));


        EditText ms = (EditText) findViewById(R.id.sdate);
        ms.setText(mnses.getStartDate());

        EditText sh = (EditText) findViewById(R.id.sDay);
        sh.setText(mnses.getShift());

        EditText bb = (EditText) findViewById(R.id.bB);
        bb.setText(mnses.getB());

        EditText bg = (EditText) findViewById(R.id.bG);
        bg.setText(mnses.getG());

        EditText nm = (EditText) findViewById(R.id.Nxt);
        nm.setText(mnses.getNxt());

        saveBtn = (Button) findViewById(R.id.savePrefs);
        saveBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                saveAndFinish();
            }
        });
    }

    private void saveAndFinish() {
        EditText mst = (EditText) findViewById(R.id.stDay);
        String mnsesStart = mst.getText().toString();

        EditText shi = (EditText) findViewById(R.id.ShiftDay);
        String shift = shi.getText().toString();

        EditText bb = (EditText) findViewById(R.id.bbB);
        String bbb = b.getText().toString();

        EditText gg = (EditText) findViewById(R.id.ggG);
        String ggg = gg.getText().toString();

        EditText nxt = (EditText) findViewById(R.id.Nxt);
        String next = nxt.getText().toString();

        Intent intent = new Intent();
        intent.putExtra("startdatekey", mnses.getStartDate());
        intent.putExtra("StartDate", mnsesStart);
        intent.putExtra("ShiftKey", mnses.getShift());
        intent.putExtra("Shift", shift);
        intent.putExtra("BKey", mnses.getB());
        intent.putExtra("B", bbb);
        intent.putExtra("GKey", mnses.getG());
        intent.putExtra("G", ggg);
        intent.putExtra("NxtKey", mnses.getNxt());
        intent.putExtra("Nxt", next);
        setResult(RESULT_OK, intent);
        finish();

    }

}

2。 MsDataSource.java

   public class MsDataSource {

        MsItem mnses;


private static final String MNS_PREFS = "mnsKEY";
public SharedPreferences mnsPrefs;

public static final String StartDate = "startKey";
public static final String Shift = "shiftKey";
public static final String B = "bKey";
public static final String G = "gKey";
public static final String Nxt = "nxtKey";  

public MsDataSource(Context context) {
    mnsPrefs = context.getSharedPreferences(MNS_PREFS, Context.MODE_PRIVATE);
}

    public String getTheStartDate() {
        String mnsstart = mensPrefs.getString(StartDate, "Not seen");
        return mnsstart;                    
    }
    public String getTheShift() {
        String shiftda = mensPrefs.getString(Ovulation, "no data");
        return shiftda;                 
    }
    public String getTheBoy() {
        String Bda = mensPrefs.getString(B, "not accessible");
        return Bda;                 
    }
    public String getTheGirl() {
        String Gda = mensPrefs.getString(G, "does not exist");
        return Gda;                 
    }
    public String getTheNextMenses() {
        String nxtmns = mensPrefs.getString(Nxt, "maybe later");
        return nxtmns;                  
    }

            public boolean update(mnsesItem mns) {
                SharedPreferences.Editor editor = mnsPrefs.edit();
                editor.putString(mns.getStartKey(), mns.getStartDate());
                editor.putString(mns.getShiftKey(), mns.getShift());
                editor.putString(mns.getBKey(), mns.getB());
                editor.putString(mns.getGKey(), mns.getG());
                editor.putString(mns.getNxtKey(), mns.getNxt());
                editor.commit();
                return true;
            }

    }

第3。 Mst.java

public class Mst extends Activity {

    private TextView startdate;
    private TextView shiftday;
    private TextView bday;
    private TextView gday;
    private TextView nextday;

    public static final String StartDate = "startKey";
    public static final String Shift = "shiftKey";
    public static final String B = "bKey";
    public static final String G = "gKey";
    public static final String Nxt = "nxtKey";

    private static final int EDITOR_ACTIVITY_REQUEST = 1001;
    private MsDataSource datasource;
    MsItem mnses;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mns_layout);

        startdate = (TextView) findViewById(R.id.mnsstartd);
        shiftday = (TextView) findViewById(R.id.shd);
        bday = (TextView) findViewById(R.id.bbd);
        gday = (TextView) findViewById(R.id.bgd);
        nextday = (TextView) findViewById(R.id.nxtmnsd);

        datasource = new MsDataSource(this);

        refreshDisplay();   
    }

    private void refreshDisplay() {
        mnses = datasource.findAll();
        startdate.setText(datasource.mnsPrefs.getString(StartDate, "not retrieved"));
        shiftday.setText(datasource.mnsPrefs.getString(Shift, "unavailable"));
        bday.setText(datasource.mnsPrefs.getString(B, "not seen"));
        gday.setText(datasource.mnsPrefs.getString(G, "not found"));
        nextday.setText(datasource.mnsPrefs.getString(Nxt, "no data"));     

    }

    private void createMns() {
        MsItem mns = MsItem.getNew();
        Intent intent = new Intent(this, MnsEditor.class);
        intent.putExtra("startKey", mns.getStartKey());
        intent.putExtra("StartDate", mns.getStartDate());
        intent.putExtra("shiftKey", mns.getShiftKey());
        intent.putExtra("Shift", mns.getShift());
        intent.putExtra("bKey", mns.getBKey());
        intent.putExtra("B", mns.getB());
        intent.putExtra("gKey", mns.getGKey());
        intent.putExtra("G", mns.getG());
        intent.putExtra("nxtKey", mns.getNxtKey());
        intent.putExtra("Nxt", mns.getNxt());

        startActivityForResult(intent, EDITOR_ACTIVITY_REQUEST);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == EDITOR_ACTIVITY_REQUEST && resultCode == RESULT_OK) {
            MsItem mns = new MsItem();
            mns.setStartKey(data.getStringExtra("startKey"));
            mns.setStartDate(data.getStringExtra("StartDate"));
            mns.setShiftKey(data.getStringExtra("shiftKey"));
            mns.setShift(data.getStringExtra("Shift"));
            mns.setBKey(data.getStringExtra("bKey"));
            mns.setB(data.getStringExtra("B"));
            mns.setGKey(data.getStringExtra("gKey"));
            mns.setG(data.getStringExtra("G"));
            mns.setNextmnsesKey(data.getStringExtra("nxtKey"));
            mns.setNxt(data.getStringExtra("Nxt"));

            datasource.update(mns);

            refreshDisplay();
        }
    }

}

4。 MsItem.java

@SuppressLint("SimpleDateFormat")
public class MsItem {

    private String startKey;
    private String StartDate;

    private String shiftKey;
    private String Shift;

    private String bKey;
    private String B;

    private String gKey;
    private String G;

    private String nxtKey;
    private String Nxt;


    public String getStartKey() {
        return startKey;
    }
    public void setStartKey(String startKey) {
        this.startKey = startKey;
    }
    public String getStartDate() {
        return StartDate;
    }
    public void setStartDate(String startDate) {
        StartDate = startDate;
    }
    public String getShiftKey() {
        return shiftKey;
    }
    public void setShiftKey(String shiftKey) {
        this.shiftKey = shiftKey;
    }
    public String getShift() {
        return Shift;
    }
    public void setShift(String shift) {
        Shift = shift;
    }
    public String getbKey() {
        return bKey;
    }
    public void setbKey(String bKey) {
        this.bKey = bKey;
    }
    public String getB() {
        return B;
    }
    public void setB(String b) {
        B = b;
    }
    public String getgKey() {
        return gKey;
    }
    public void setgKey(String gKey) {
        this.gKey = gKey;
    }
    public String getG() {
        return G;
    }
    public void setG(String g) {
        G = g;
    }
    public String getNxtKey() {
        return nxtKey;
    }
    public void setNxtKey(String nxtKey) {
        this.nxtKey = nxtKey;
    }
    public String getNxt() {
        return Nxt;
    }
    public void setNxt(String nxt) {
        Nxt = nxt;
    }


    public static MsItem getNew(){

        Locale locale = new Locale("en_US");
        Locale.setDefault(locale);

        String pattern = "yyyy-MM-dd HH:mm:ss Z";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String startdatekey = formatter.format(new Date());

        String skey = formatter.format(new Date());

        String bkey = formatter.format(new Date());

        String gkey = formatter.format(new Date());

        String nkey = formatter.format(new Date());

        MsItem mnses = new MsItem();
        mnses.setStartKey(startdatekey);
        mnses.setStartDate("");

        mnses.setShiftKey(skey);
        mnses.setShift("");

        mnses.setbKey(bkey);
        mnses.setB("");

        mnses.setgKey(gkey);
        mnses.setG("");

        mnses.setNxtKey(nkey);
        mnses.setNxt("");
        return mnses;
    }

}

3 个答案:

答案 0 :(得分:0)

我可以看到的一个问题是,您正在尝试获取sharedPreferences,但是您正在MsDataSource.java类中初始化相同的对象几次:

    mnsPrefs = context.getSharedPreferences(StartDate, Context.MODE_PRIVATE);
    mnsPrefs = context.getSharedPreferences(shift, Context.MODE_PRIVATE);
    mnsPrefs = context.getSharedPreferences(B, Context.MODE_PRIVATE);
    mnsPrefs = context.getSharedPreferences(G, Context.MODE_PRIVATE);
    mnsPrefs = context.getSharedPreferences(Nxt, Context.MODE_PRIVATE);

所以这不起作用。对于不同的sharedPref对象,您需要将它们初始化为唯一的,例如:

      public SharedPreferences mnsPrefs1;
      public SharedPreferences mnsPrefs2;
      public SharedPreferences mnsPrefs3;
      public SharedPreferences mnsPrefs4;
      public SharedPreferences mnsPrefs5;


    mnsPrefs1 = context.getSharedPreferences(StartDate, Context.MODE_PRIVATE);
    mnsPrefs2 = context.getSharedPreferences(shift, Context.MODE_PRIVATE);
    mnsPrefs3 = context.getSharedPreferences(B, Context.MODE_PRIVATE);
    mnsPrefs4 = context.getSharedPreferences(G, Context.MODE_PRIVATE);
    mnsPrefs5 = context.getSharedPreferences(Nxt, Context.MODE_PRIVATE);

但必须有更多。我需要在MsItem.java类中看到你的代码,我想,你在这里保存SharedPrefs,对吗?

修改

您不需要初始化这么多的SharedPreference对象,只需要一个,并为每个值赋予不同的键。例如:

private final String sharedPrefKey =“MY_KEY”;

   public MsDataSource(Context context) {
    mnsPrefs = context.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE);

   }

然后MsDataSource.java中的update()方法应该可以工作。但顺便说一下,我认为你在onActivityResult的Mst.java类中初始化你的MsItem是以错误的方式进行的,不是吗?你写了

   MsItem mns = new MsItem();

但不是吗

   MsItem mns = MsItem.getNew();

?因为只有新的MsItem(),所有的字符串和东西都没有在MsItem中初始化。

答案 1 :(得分:0)

试试这个:

public String getStartDate(){
       mnsPrefs = context.getSharedPreferences(StartDate, Context.MODE_PRIVATE);
      String mnsstart = mnsPrefs.getString(StartDate, "");
      return mnsstart;
}

 public String getShift(){
       mnsPrefs = context.getSharedPreferences(shift, Context.MODE_PRIVATE);
      String shiftda = mnsPrefs.getString(Shift, "");
      return shiftda;
}

更具可读性

答案 2 :(得分:0)

使用MODE_APPEND而不是MODE_PRIVATE。

MODE_APPEND(文件创建模式:用于openFileOutput,如果文件已经存在,则将数据写入现有文件的末尾而不是删除它。)

  SharedPreferences editor = context.getSharedPreferences(
            "used_name", Context.MODE_APPEND);

  editor.putString("username", value);
  editor.commit();