• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Code Help

Hi I am creating a task reminder app. It works and I have also created a Toast validation. For example a user doesn't fill in the title and the toast says "title needs to be filled in!" e.t.c
But the file still saves even after the toast message. How do I ensure the user has to fill in the fields in order to save the message?

Thanks





This is my method.

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(this).setReminder(mRowId, mCalendar);

if(mTitleText.length() > 0)
{
// your method logic
} else{
// You notify the user he needs to fill title field
Toast.makeText(getApplicationContext(), "Title needs to be filled in!", Toast.LENGTH_SHORT).show();

}
if(mBodyText.length() > 0)
{
// your method logic
} else{
// You notify the user he needs to fill title field
Toast.makeText(getApplicationContext(), "Body needs to be filled in!", Toast.LENGTH_SHORT).show();

}



}
 
I am wondering if you understand this code, because it doesn't seem to be programmed in a logical way...

You should do the checks before doing anything with reminders.
In your code, mBodyText, seems to be a mistake. It looks like you could use title better.. Same for mBodyText
 
Hi this is the actual method before I edited it.





if (mRowId == null) {

long id = mDbHelper.createReminder(title, body,

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());





reminderDateTime);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
}

new ReminderManager(this).setReminder(mRowId, mCalendar);



}

}





I'd just like for it to be able to check if the fields are empty, if the user as entered the fields, then the user is able to save.
 
This is the new code now:


package com.driver.android.taskreminder;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class ReminderEditActivity extends Activity {

//
// Dialog Constants
//
private static final int DATE_PICKER_DIALOG = 0;
private static final int TIME_PICKER_DIALOG = 1;

//
// Date Format
//
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "kk:mm";
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";

private EditText mTitleText;
private EditText mBodyText;
private Button mDateButton;
private Button mTimeButton;
private Button mConfirmButton;
private Long mRowId;
private RemindersDbAdapter mDbHelper;
private Calendar mCalendar;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mDbHelper = new RemindersDbAdapter(this);

setContentView(R.layout.reminder_edit);

mCalendar = Calendar.getInstance();
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
mDateButton = (Button) findViewById(R.id.reminder_date);
mTimeButton = (Button) findViewById(R.id.reminder_time);

mConfirmButton = (Button) findViewById(R.id.confirm);

mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID)
: null;

registerButtonListenersAndSetDefaultText();
}

private void setRowIdFromIntent() {
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(RemindersDbAdapter.KEY_ROWID)
: null;

}
}


protected void onPause() {
super.onPause();
mDbHelper.close();
}


protected void onResume() {
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}


protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_PICKER_DIALOG:
return showDatePicker();
case TIME_PICKER_DIALOG:
return showTimePicker();
}
return super.onCreateDialog(id);
}

private DatePickerDialog showDatePicker() {


DatePickerDialog datePicker = new DatePickerDialog(ReminderEditActivity.this, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDateButtonText();
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
return datePicker;
}

private TimePickerDialog showTimePicker() {

TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true);

return timePicker;
}

private void registerButtonListenersAndSetDefaultText() {

mDateButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(DATE_PICKER_DIALOG);
}
});


mTimeButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});

mConfirmButton.setOnClickListener(new View.OnClickListener() {



public void onClick(View view) {
saveState();
setResult(RESULT_OK);
Toast.makeText(ReminderEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();

}

});

updateDateButtonText();
updateTimeButtonText();
}

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);
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(this);
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));

}

updateDateButtonText();
updateTimeButtonText();

}
private void updateTimeButtonText() {
// Set the time button text based upon the value from the database
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeForButton = timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}

private void updateDateButtonText() {
// Set the date button text based upon the value from the database
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}


protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);
}



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(mBodyText.length() > 0)
{


//saving method goes here




} else{
// Notifies the user that he needs to fill in Title field
Toast.makeText(getApplicationContext(), "Title needs to be filled in!", Toast.LENGTH_SHORT).show();

}

if (mRowId == null) {

long id = mDbHelper.createReminder(title, body, reminderDateTime);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
}

new ReminderManager(this).setReminder(mRowId, mCalendar);



}

}
 
As I said, you should do the checks before doing anything else, like
Code:
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();

if (title.length() == 0) {
    Toast.makeText(getApplicationContext(), "Title needs to be filled in!", Toast.LENGTH_SHORT).show();
} else if (body.length() == 0) {
    Toast.makeText(getApplicationContext(), "Body needs to be filled in!", Toast.LENGTH_SHORT).show();
else {
    // rest of your function
}
 
Back
Top Bottom