How the datepickerdialog date you choose remains after the activity ends

73 Views Asked by At

Sorry for the Google translator

What I want to implement is that the date that the user chooses is maintained even when the activity ends. So I hope the settings are retained when I rerun the activity.

I tried SharedPreferences, String succeeded but datapicker failed What can I do to keep my data picker user selections?

This is the code I used.

public class Widget extends Activity {
/**
 * Called when the activity is first created.
 */

private TextView ddayText;
private TextView todayText;
private TextView resultText;
private TextView nameText;
private Button dateButton;


private int tYear;          
private int tMonth;
private int tDay;

private int dYear = 1;       
private int dMonth = 1;
private int dDay = 1;


private long d;
private long t;
private long r;
private long w;

private int resultNumber = 0;
private int dayNumber = 0;

private SharedPreferences sf;

static final int DATE_DIALOG_ID = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widget);

    ddayText = (TextView) findViewById(R.id.dday);
    todayText = (TextView) findViewById(R.id.today);
    resultText = (TextView) findViewById(R.id.result);
    dateButton = (Button) findViewById(R.id.dateButton);
    nameText = (TextView) findViewById(R.id.name);

    dateButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(0);//----------------

        }
    });


    sf = getSharedPreferences("sf", Activity.MODE_PRIVATE);
    String str = sf.getString("name", ""); 
    long dday = sf.getLong("dday", d); 

    nameText.setText(str); 



    Calendar calendar = Calendar.getInstance();             
    tYear = calendar.get(Calendar.YEAR);
    tMonth = calendar.get(Calendar.MONTH);
    tDay = calendar.get(Calendar.DAY_OF_MONTH);

    Calendar dCalendar = Calendar.getInstance();
    dCalendar.set(dYear, dMonth, dDay);

    t = calendar.getTimeInMillis();                 
    d = dCalendar.getTimeInMillis();             
    r = (d - t) / (7 * 24 * 60 * 60 * 1000L);  
    w = (d - t) / (24 * 60 * 60 * 1000L); 


    resultNumber = (int) r * (-1);
    dayNumber = (int) w * (-1);

    updateDisplay();


}//OnCreate end


private void updateDisplay() {

    todayText.setText(String.format("%d년 %d월 %d일", tYear, tMonth + 1, tDay));
    ddayText.setText(String.format("%d년 %d월 %d일", dYear, dMonth + 1, dDay));

    if (resultNumber >= 0) {
        resultText.setText(String.format("임신 %d주 %d일", resultNumber % 45L, dayNumber % 7L + 1));


    } else {

        int absR = Math.abs(resultNumber);
        resultText.setText(String.format("임신 %d주 %d일", absR, dayNumber % 7L));


    }
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == DATE_DIALOG_ID) {
        return new DatePickerDialog(this, dDateSetListener, tYear, tMonth, tDay);
    }

    return null;
}

private DatePickerDialog.OnDateSetListener dDateSetListener = new DatePickerDialog.OnDateSetListener() {


    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        final Calendar dCalendar = Calendar.getInstance();
        dCalendar.set(year, monthOfYear, dayOfMonth);

        long d = dCalendar.getTimeInMillis();
        r = (d - t) / (7 * 24 * 60 * 60 * 1000L);
        w = (d - t) / (24 * 60 * 60 * 1000L);

        resultNumber = (int) r * (-1);
        dayNumber = (int) w * (-1);

        saveData(nameText.getText().toString(), d);

        updateDisplay();

    }
    private void saveData(String str, long d) {

        SharedPreferences.Editor editor = sf.edit();

        editor.putString("name", str);
        editor.putLong("dday", d);

        editor.commit();
    }
};

}

1

There are 1 best solutions below

10
On BEST ANSWER

I think you'll want to save data within onDateSet to the SharedPreferences if you want to store it at the correct time.

For example, move the preferences to a field

// Store this as a field for later reference
private SharedPreferences sf;
private final Calendar mCalendar = Calendar.getInstance();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widget);

    nameText = (TextView) findViewById(R.id.name);

    // Load any data you have 
    sf = getSharedPreferences("sf", Activity.MODE_PRIVATE);
    String str = sf.getString("name", ""); 
    long dday = sf.getLong("dday", -1); 
    // TODO: set mCalendar time using dday
    // if dday == -1, then nothing is saved 

    // load the date picker values 
    tYear = mCalendar.get(Calendar.YEAR);
    tMonth = mCalendar.get(Calendar.MONTH);
    tDay = mCalendar.get(Calendar.DAY_OF_MONTH);

    nameText.setText(str); 
    updateDisplay();

And use it within the listener. Add a helper method to extract out the logic that writes to the preferences

private DatePickerDialog.OnDateSetListener dDateSetListener = new DatePickerDialog.OnDateSetListener() {


    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        mCalendar.set(year, monthOfYear, dayOfMonth);
        long d = mCalendar.getTimeInMillis();

        saveData(nameText.getText().toString(), d);
        // updateDisplay();
}

private void saveData(String str, long d) {
    SharedPreferences.Editor editor = sf.edit();

    editor.putString("name", str); 
    editor.putLong("dday", d); 

    editor.commit(); 
}