2017-06-16 40 views
1

我正在使用庫材質日曆視圖實現日曆應用程序。該應用程序是一個日記,用戶可以選擇日期,在那裏他可以更改背景圖像,從圖庫中放置圖像或製作照片和表情符號雨以獲得樂趣。我的問題是如何在每天保存「標題」和「描述」(editText)?當用戶關閉應用程序並再次打開它時,我希望輸入的文本位於用戶輸入其信息的特定日期。如何保存用戶在每天使用庫材質日曆視圖在EditText中輸入的內容?

我試着用共享首選項來保存標題和描述,但信息保存在整個日曆日,我不知道如何在每個日期保存。是否有物料日曆視圖的解決方案? 我將數據保存到一個包中,然後將其傳遞給「Days」活動,這裏我希望白天保存標題和描述。

活動準則「日曆」

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_calendars); 

     MaterialCalendarView materialCalendarView = (MaterialCalendarView) findViewById(calendarView); 
     materialCalendarView.state().edit() 
       .setFirstDayOfWeek(Calendar.MONDAY) 
       .setMinimumDate(CalendarDay.from(1900, 1, 1)) 
       .setMaximumDate(CalendarDay.from(2100, 12, 31)) 
       .setCalendarDisplayMode(CalendarMode.MONTHS) 
       .commit(); 
     //set the current day to be highlighted 
     Calendar calendar = Calendar.getInstance(); 
     materialCalendarView.setDateSelected(calendar.getTime(), true); 

     materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() { 
      @Override 
      public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) { 
       //Toast.makeText(Calendars.this, " " + date, Toast.LENGTH_LONG).show(); 
       Intent intentBundle = new Intent(Calendars.this, Days.class); 
       Bundle bundle = new Bundle(); 
       bundle.putString("Data", String.valueOf(date)); 
       intentBundle.putExtras(bundle); 
       startActivity(intentBundle); 
      } 
     }); 


    } 

而且,我想通過每天節省標題和描述的活動天數的代碼:

private EditText txtTitle; 
    private EditText txt_description; 
    private Button btnSave; 
    private EditText txtDate; 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_days); 

     //for saving data initialize the views 
     btnSave = (Button) findViewById(R.id.btnSave); 
     txtTitle = (EditText) findViewById(R.id.txtTitle); 
     txt_description = (EditText) findViewById(R.id.txt_description); 
     //adding click event on button save 
     btnSave.setOnClickListener(this); 
     loadSavedPreferences(); 
     //get bundle open 
     Bundle bundle = getIntent().getExtras(); 
     //Extract the data 
     String data = bundle.getString("Data"); 
     //Create the text wiew 
     TextView textView = (TextView) this.findViewById(R.id.txtDate); 
     textView.setTextSize(17); 
     textView.setText(data); 
} 

//for saving data 
    private void loadSavedPreferences(){ 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     String title = sharedPreferences.getString("storedTitle", "Enter title"); 
     String description = sharedPreferences.getString("storedDescription", "Enter description"); 
     txtTitle.setText(title); 
     txt_description.setText(description); 


    } 
    private void savePreferences(String key, String value){ 
     SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(key,value); 

     editor.apply(); 
    } 

代碼按鈕保存

case R.id.btnSave: 
       savePreferences("storedTitle", txtTitle.getText().toString()); 
       savePreferences("storedDescription", txt_description.getText().toString()); 
       Toast.makeText(Days.this, 
       "Data was saved!", Toast.LENGTH_SHORT) 
       .show(); 

回答

0

您可以使用數據庫,例如,包含日期和標題以及任何其他東西的對象保存。有一些很好的ORM,比如SugarORM,DBFlow,Realm,並且像這樣

+0

或者簡單地使用'sqlite' –

相關問題