3

I'm developing an android app for babies and after the user sets the birth date of the baby, i need to save it so i can use it in another activity such as sleep and vaccinations. I have used the Shared Preference to save the data enters by the user. but i haven't figure out how to get that data and use it in other classes. can u help me please?

here is the code for the baby data required

public class MainActivity extends Activity {
private Button change;
private ImageView bImage;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
     // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        updateDateDisplay();


}
   private void updateDateDisplay() {

        this.mPickDate.setText(
        new StringBuilder()
        .append(mMonth + 1).append("-")
        .append(mDay).append("-")
        .append(mYear).append(" "));
        }
   public int getAge (int _year, int _month, int _day) {

       GregorianCalendar cal = new GregorianCalendar();
       int y, m, d, a;         

       y = cal.get(Calendar.YEAR);
       m = cal.get(Calendar.MONTH) + 1;
       d = cal.get(Calendar.DAY_OF_MONTH);
       cal.set(_year, _month, _day);
       a = y - cal.get(Calendar.YEAR);
       if ((m < cal.get(Calendar.MONTH))
                       || ((m == cal.get(Calendar.MONTH)) && (d < cal
                                       .get(Calendar.DAY_OF_MONTH)))) {
               --a;
       }
       if(a < 0)
               throw new IllegalArgumentException("Age < 0");
       return a;
       }



    // the callback received when the user “sets” the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year,
    int monthOfYear, int dayOfMonth) {
    mYear = year;
    mMonth = monthOfYear;
    mDay = dayOfMonth;
    updateDateDisplay();
    }

    };


    @Override
    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
    return new DatePickerDialog(this,
    mDateSetListener,
    mYear, mMonth, mDay);
    }
    return null;
    }
4

2 回答 2

0

由于我没有在您的代码中看到您创建的位置SharedPreference,因此我将为您提供文档中的示例

public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){
   super.onCreate(state);
   . . .

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);  // get it here using the name
   boolean silent = settings.getBoolean("silentMode", false);  get a value using the key
   setSilent(silent);
}

您只需使用保存时创建的首选项的名称。此示例检索 aboolean但您可以轻松地将其更改为integerorString或您需要的任何内容

于 2013-05-07T19:13:13.410 回答
0

用于访问共享首选项->

用于在首选项中保存String数据 ->

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor e = prefs.edit();
e.putString(key, val);   //Key - Name with which you want to save pref, val -  value to save
e.commit();

从首选项获取String数据->

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String s = prefs.getString(key, def); // Key - Name of prefs, def - default value if prefs with specified key so not exists

您可以在首选项中保存String, Float, Long,IntBoolean

于 2013-05-07T19:16:19.677 回答