我是android開發新手。我希望你能幫助我如何將從dialogfragment檢索到的數據保存到我的數據庫中。我目前的代碼能夠保存數據,但只有當前日期不是所選的日期。如何將從DatePicker檢索到的數據保存到數據庫?
public class RemindersFragment extends Fragment {
private EditText mTitleText;
private EditText mBodyText;
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
private Button mConfirmButton;
private Long mRowId;
private RemindersDbAdapter mDbHelper;
private Calendar mCalendar;
Button btnChangeDate,btnChangeTime;
TextView txtDisplayDate,txtDisplayTime;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDbHelper = new RemindersDbAdapter(getActivity());
View rootView = inflater.inflate(R.layout.fragment_reminders, container, false);
mCalendar = Calendar.getInstance();
mTitleText = (EditText) rootView.findViewById(R.id.title);
mBodyText = (EditText) rootView.findViewById(R.id.body);
mConfirmButton = (Button) rootView.findViewById(R.id.confirm);
btnChangeDate = (Button) rootView.findViewById(R.id.btnChangeDate);
btnChangeTime = (Button) rootView.findViewById(R.id.btnChangeTime);
txtDisplayTime = (TextView) rootView.findViewById(R.id.txtTime);
txtDisplayDate = (TextView) rootView.findViewById(R.id.txtDate);
mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID)
: null;
registerButtonListenersAndSetDefaultText();
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
btnChangeTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
}
});
return rootView;
}
// display current date
public void showCurrentDateOnView() {
year = mCalendar.get(Calendar.YEAR);
month = mCalendar.get(Calendar.MONTH);
day = mCalendar.get(Calendar.DAY_OF_MONTH);
// set current date into textview
txtDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
}
// display current time
public void showCurrentTimeOnView() {
hour = mCalendar.get(Calendar.HOUR_OF_DAY);
minute = mCalendar.get(Calendar.MINUTE);
// set current time into textview
txtDisplayTime.setText(
new StringBuilder().append(hour)
.append(":").append(minute));
}
private void setRowIdFromIntent() {
if (mRowId == null) {
Bundle extras = getActivity().getIntent().getExtras();
mRowId = extras != null ? extras.getLong(RemindersDbAdapter.KEY_ROWID)
: null;
}
}
public void showDatePickerDialog() {
DatePickerFragment newFragment = new DatePickerFragment(txtDisplayDate);
newFragment.show(getFragmentManager(), "datePicker");
}
public void showTimePickerDialog() {
TimePickerFragment newFragment = new TimePickerFragment(txtDisplayTime);
newFragment.show(getFragmentManager(), "timePicker");
}
@Override
public void onPause() {
super.onPause();
mDbHelper.close();
}
@Override
public void onResume() {
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}
private void registerButtonListenersAndSetDefaultText() {
mConfirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent;
saveState();
getActivity().setResult(Activity.RESULT_OK);
Toast.makeText(getActivity(), getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
intent = new Intent(getActivity().getApplication(), Manage_Settings.class);
startActivity(intent);
}
});
showCurrentDateOnView();
showCurrentTimeOnView();
}
private void populateFields() {
// Only populate the text boxes and change the calendar date
// if the row is not null from the database.
if (mRowId != null) {
Cursor reminder = mDbHelper.fetchReminder(mRowId);
getActivity().startManagingCursor(reminder);
mTitleText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_TITLE)));
mBodyText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_BODY)));
// Get the date from the database and format it for our use.
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
Date date = null;
try {
String dateString = reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_DATE_TIME));
date = dateTimeFormat.parse(dateString);
mCalendar.setTime(date);
} catch (ParseException e) {
Log.e("ReminderEditActivity", e.getMessage(), e);
}
} else {
// This is a new task - add defaults from preferences if set.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String defaultTitleKey = getString(R.string.pref_task_title_key);
String defaultTimeKey = getString(R.string.pref_default_time_from_now_key);
String defaultTitle = prefs.getString(defaultTitleKey, null);
String defaultTime = prefs.getString(defaultTimeKey, null);
if(defaultTitle != null)
mTitleText.setText(defaultTitle);
if(defaultTime != null)
mCalendar.add(Calendar.MINUTE, Integer.parseInt(defaultTime));
}
showCurrentDateOnView();
showCurrentTimeOnView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);
}
@SuppressLint("SimpleDateFormat")
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());
if (mRowId == null) {
long id = mDbHelper.createReminder(title, body, reminderDateTime);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
}
new ReminderManager(getActivity()).setReminder(mRowId, mCalendar);
}
}
這裏是我的日期選擇DialogFragment:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
TextView txtDate;
public DatePickerFragment(TextView txtDate) {
super();
this.txtDate = txtDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
//DatePickerFragmentListener listener = (DatePickerFragmentListener) getTargetFragment();
txtDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
}
什麼我迄今所做的:
我試圖設置mCalendar在當前日期,但我不認爲我正確的做法:
// display current date
public void showCurrentDateOnView() {
year = mCalendar.get(Calendar.YEAR);
month = mCalendar.get(Calendar.MONTH);
day = mCalendar.get(Calendar.DAY_OF_MONTH);
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
// set current date into textview
txtDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
}
任何幫助將不勝感激。
謝謝!你是一個拯救生命的人:) – DEADPOOL 2014-09-02 23:55:17