2013-05-07 64 views
3

我正在開發一款適用於嬰兒的android應用程序,在用戶設置了寶寶的出生日期後,我需要保存它,以便在睡眠和接種疫苗等其他活動中使用它。 我已經使用Shared Preference來保存用戶輸入的數據。 但我還沒有弄清楚如何獲取該數據並在其他類中使用它。你能幫我嗎?如何獲得年齡並將其用於其他活動?

這裏是寶寶數據的代碼需要

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; 
    } 
+0

獲取年齡,然後使用意圖在活動之間傳遞數據http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488 #15859488 – Raghunandan 2013-05-07 19:07:24

+0

或打開應用程序的相同共享首選項nd讀取保存在其中的日期.. – Kamal 2013-05-07 19:08:52

+1

爲什麼不通過一個意圖將數據傳遞給其他Activity?似乎它會比使用SharedPrefs一個數據 – 2013-05-07 19:14:49

回答

0

因爲我沒有在你的代碼看到你創建SharedPreference,我會給你從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); 
} 
的例子

您只需使用您在保存時創建的首選項的名稱。這個例子檢索boolean,但你可以很容易地改變,要integerString或任何你需要

+0

非常感謝你我會嘗試it.i已經做了另一個類,只擴展偏好活動,我已經添加到它我的xml佈局,其中包含的數據feilds和它的工作它在輸入數據時確實會保存數據。好的,如果我想跟蹤年齡並將其更新到用戶,我應該添加什麼? – user2330447 2013-05-08 00:59:37

+0

好的,現在我已經制作了一個全新的代碼,將出生日期寫爲編輯文本,這裏是我的pref.xml的代碼 – user2330447 2013-05-08 05:00:36

0

訪問共享Prefs->

對於首選項保存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,Int,Boolean等。

相關問題