2016-10-22 12 views
0

設定した日付から今日までの日数をカウントし、ユーザがログインするたびにカウンタを更新しようとしているアクティビティを作成しましたが、うまくいくようです。アクティビティの開始時に日付カウンタを更新する方法

以下

は、私はそれを達成しようとしている方法を反映して、私のスクリプトです:

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.appactivity); 
    DateDisplay=(TextView)findViewById(R.id.data); 
    ContoGiorni=(TextView)findViewById(R.id.textViewConto); 
    update(); 
    updateConto(); 
} 

protected void update(){ 

    SharedPreferences prefs=getSharedPreferences(DATA_PREFERENCIES, Context.MODE_PRIVATE); 
    String giorno=prefs.getString(GIORNO,"00"); 
    String mese=prefs.getString(MESE,"00"); 
    String anno=prefs.getString(ANNO,"0000"); 
    DateDisplay.setText(
      new StringBuilder() 
       .append(anno).append("-") 
       .append(mese).append("-") 
       .append(giorno) 
    ); 
} 

protected void updateConto(){ 

    SharedPreferences prefs=getSharedPreferences(DATA_PREFERENCIES, Context.MODE_PRIVATE); 
    Calendar calendar=new GregorianCalendar(); 
    calendar=Calendar.getInstance(); 
    long millisecondsToday=calendar.getTimeInMillis(); 
    String millisecond=prefs.getString(MILLISECOND,"0"); 
    long millisecondLong=Long.parseLong(millisecond); 
    long diff = millisecondsToday - millisecondLong; 
    long sec = diff/1000 ; 
    long minutes = sec/60 ; 
    long hr = minutes/60 ; 
    long days = hr/24 ; 
    SharedPreferences.Editor editor=prefs.edit(); 
    editor.putString(CONTO,String.valueOf(days)); 
    String conto=prefs.getString(CONTO,"0"); 
    ContoGiorni.setText(
      new StringBuilder().append(conto).append(" giorni con te")); 
} 

public void setData (View v){ 

    showDialog(0); 
} 

protected DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 

     SharedPreferences prefs=getSharedPreferences(DATA_PREFERENCIES, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor=prefs.edit(); 
     annoMio=year; 
     meseMio=month; 
     giornoMio=day; 
     editor.putString(GIORNO, String.valueOf(annoMio)); 
     editor.putString(MESE,String.valueOf(meseMio+1)); 
     editor.putString(ANNO,String.valueOf(giornoMio)); 

     Calendar calOne=new GregorianCalendar(); 
     Calendar calTwo=new GregorianCalendar(); 
     calOne = Calendar.getInstance(); 
     calTwo = Calendar.getInstance(); 
     calTwo.set(annoMio, meseMio, giornoMio); 
     long milliseconds1 = calOne.getTimeInMillis(); 
     long milliseconds2 = calTwo.getTimeInMillis(); 
     editor.putString(MILLISECOND,String.valueOf(milliseconds2)); 
     long diff = milliseconds1 - milliseconds2; 
     long sec = diff/1000 ; 
     long minutes = sec/60 ; 
     long hr = minutes/60 ; 
     long days = hr/24 ; 
     editor.putString(CONTO,String.valueOf(days)); 

     editor.apply(); 
     update(); 
     updateConto(); 
    } 
}; 

私が間違っているつもりだとどのように私はそれを達成することができますどこに? updateContoで

答えて

0

()、あなたはSharedPreferences

editor.putString(コント、String.valueOf(日))に書いている

が、次の行に、あなたはそれ

をフェッチ

ストリングコント= prefs.getString(コント、 "0")

は、エディタ#を呼び出しずに適用されます。したがって、その環境設定への変更は永続することはありません。

関連する問題