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;
    }
  • Get the age and then use intent to pass data between activities http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488#15859488 – Raghunandan May 07 '13 at 19:07
  • or open the same shared preferences of the application nd read the date saved in it.. – Kamal May 07 '13 at 19:08
  • 1
    Why not just pass the data to the other Activity via an Intent? Seems like it would be much simpler than using SharedPrefs for one piece of data – Jade Byfield May 07 '13 at 19:14
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Jean-Bernard Pellerin May 08 '13 at 02:17

2 Answers2

0

Since I don't see in your code where you create the SharedPreference, I will give you the example from the Docs

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);
}

You simply use the name of the prefs that you created when you saved it. This example retrieves a boolean but you can easily change that to integer or String or whatever you need

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • thank u very much i'll try it.i have made another class which only extends preference activity and i had add to it my xml layout that contains the data feilds and it worked it really saves the data when it's entered. Ok what about if i want to track the age and keep update it to the user what should i add exactly? – user2330447 May 08 '13 at 00:59
  • ok, now i have made a whole new code that takes the date of birth written as an edit text here is the code of my pref.xml – user2330447 May 08 '13 at 05:00
0

For accessing Shared Prefs->

For saving String data in prefs ->

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();

For getting String data from prefs ->

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

You can save String, Float, Long, Int, Boolean etc in prefs.

Vishal Pawale
  • 3,416
  • 3
  • 28
  • 32