Back to project page ProjectStudio.
The source code is released under:
Apache License
If you think the Android project ProjectStudio listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** *//from w w w . j a v a2 s . c om */ package fragments; import android.app.ActionBar; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.example.uniutilproject.R; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import DB_Provider.CourseDataSource; import DB_Provider.DB_ABSTRACTS; import DB_Provider.ProfessorDataSource; import DB_Provider.TaskDataSource; import dialog_fragments.DatePickerDialogFragment; import dialog_fragments.TimePickerDialogFragment; import model.Professor; import model.Task; /** * @author desmond */ public class AddProfFragment extends Fragment { public static final int DATEPICKER_FRAGMENT = 0; public static final int TIMEPICKER_FRAGMENT = 1; private DrawerLayout parent_drawer; private EditText add_prof_name_et; private EditText add_prof_email_et; private AutoCompleteTextView add_course_et; private CheckBox book_meeting_cb; private Spinner meeting_day; private TextView meeting_time; private TextView cancel_btn; //nb this is not a button private TextView save_btn; //nb this is not a button private LinearLayout meeting_view; private String date_String; private ProfessorDataSource professorDataSource; private TaskDataSource taskDataSource; private Date date_format; private Professor prof; private EditText task_location; private Task task; private static boolean meeting_booked_with_prof = false; private Date time_format; private String day_selected; private CourseDataSource courseDataSource; private LinearLayout acbar_save_button; private LinearLayout acbar_cancel_button; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.professor_addview, container, false); //CREATE A NEW PROFESSOR INSTANCE prof = new Professor(); task = new Task(); //CALL DATABASE_S professorDataSource = new ProfessorDataSource(getActivity()); taskDataSource = new TaskDataSource(getActivity()); courseDataSource = new CourseDataSource(getActivity()); try { professorDataSource.open(); taskDataSource.open(); courseDataSource.open(); } catch (SQLException e) { e.printStackTrace(); } //TO PREVENT VIEWS FROM OVERLAYING EACH OTHER WHEN //KEYPAD IS CALLED, CALL THIS METHOD getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); //DRAWER HAS TO BE DISABLED IN OTHER TO PREVENT CHILD FRAGMENTS FROM OVERLAYING PARENT FRAGMENT //parent_drawer = new DrawerLayout(this.getActivity()); //parent_drawer.setId(0x1000); parent_drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout); parent_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //INFLATE VIEWS HERE if (view != null) { add_prof_name_et = (EditText) view.findViewById(R.id.professor_add_name); add_prof_email_et = (EditText) view.findViewById(R.id.professor_add_email); add_course_et = (AutoCompleteTextView) view.findViewById(R.id.professor_add_course); book_meeting_cb = (CheckBox) view.findViewById(R.id.professor_add_checkbox); meeting_view = (LinearLayout) view.findViewById(R.id.professor_add_linearview); meeting_day = (Spinner) view.findViewById(R.id.task_dateView); meeting_time = (TextView) view.findViewById(R.id.task_timeView); //cancel_btn = (TextView) view.findViewById(R.id.task_cancelbtn); //save_btn = (TextView) view.findViewById(R.id.task_savebtn); task_location = (EditText) view.findViewById(R.id.meeting_location); } //Load day spinner ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.day_spinner, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); meeting_day.setAdapter(adapter); meeting_view.setVisibility(View.INVISIBLE); //PUT LISTENERS HERE book_meeting_cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (book_meeting_cb.isChecked()) { meeting_booked_with_prof = true; meeting_view.setVisibility(View.VISIBLE); } else { meeting_view.setVisibility(View.INVISIBLE); meeting_booked_with_prof = false; } } }); //meeting_day.setOnClickListener(new DatePickerListener()); meeting_time.setOnClickListener(new TimePickerListener()); //SET SPINNER LISTENER meeting_day.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { day_selected = parent.getItemAtPosition(position).toString(); //Toast.makeText(getActivity(), day_selected + " selected", Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); //ADD AUTOCOMLETE TO THE COURSE TEXTFIELD final ArrayList<String> list = new ArrayList<String>(); new android.os.Handler().post(new Runnable() { @Override public void run() { final Cursor cursor = courseDataSource.getAllCourses(); if (cursor.moveToFirst()) { do { list.add(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN))); } while (cursor.moveToNext()); } ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list); add_course_et.setAdapter(ad); } }); /* cancel_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // // GO BACK TO PREVIOUS FRAGMENT getActivity().onBackPressed(); } }); */ /* save_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //ASSURE THAT NO EMPTY FIELDS ARE ENTERED if (add_prof_name_et.getText().toString().equals("")) { add_prof_name_et.post(new Runnable() { public void run() { add_prof_name_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_prof_name_et, 0); toast_empty_field("NAME FIELD CANNOT BE EMPTY"); } }); } else if (add_course_et.getText().toString().equals("")) { add_course_et.post(new Runnable() { public void run() { add_course_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_course_et, 0); toast_empty_field("COURSE FIELD CANNOT BE EMPTY"); } }); } else if (add_prof_name_et.getText().toString().equals("") && add_course_et.getText().toString().equals("")) { add_prof_name_et.post(new Runnable() { public void run() { add_prof_name_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_prof_name_et, 0); toast_empty_field("EMPTY FIELDS"); } }); } else { if (time_format == null || meeting_time.getText().toString().equalsIgnoreCase("Now")){ //GET CURRENT TIME getCurrentTime(); } prof.setProfessor_name(add_prof_name_et.getText().toString()); prof.setProfessor_mail(add_prof_email_et.getText().toString()); prof.setProfessor_course(add_course_et.getText().toString()); //INSERT INTO PROFESSOR DATABASE long prof_id = professorDataSource.createProfessor(prof); //LOG THE NUMBER OF PROFESSORS Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + professorDataSource.getProfessorCount()); if (meeting_booked_with_prof) { //FOR DEBUGGING PURPOSE Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + meeting_booked_with_prof + " " + time_format + meeting_time.getText().toString()); Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + meeting_booked_with_prof + " " + day_selected); //INSERT THE DATA INTO MEETING DB task.setTime(time_format); task.setDay(day_selected); task.setNote("Meeting with " + prof.getProfessor_name()); task.setTitle("Meeting"); task.setLocation(task_location.getText().toString()); long id = taskDataSource.createTask(task, prof); //LOG THE NUMBER OF MEETINGS Log.e("MEETING COUNT", "MEETING COUNT: " + taskDataSource.getTaskCount()); } //GO BACK TO THE PREVIOUS ACTIVITY getActivity().onBackPressed(); } } }); */ //CLOSE DATABASE CONNECTIONS professorDataSource.close(); taskDataSource.close(); courseDataSource.close(); return view; } //PRINT CUSTOM TOAST MESSAGE FOR EDITTEXT public void toast_empty_field(String message) { Toast toast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT); toast.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION | Gravity.CENTER_HORIZONTAL, 0, 0); toast.show(); } //SET CURRENT DATE public void getCurrentDate(){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 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); try { date_format = dateFormat.parse(year + "-" + (month + 1) + "-" + day); } catch (ParseException e) { e.printStackTrace(); } } //SET CURRENT TIME public void getCurrentTime(){ SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); try { time_format = timeFormat.parse(hour + ":" + minute); } catch (ParseException e) { e.printStackTrace(); } } // SHOW THE DATEPICKER DIALOG FRAGMENT public void showDatePickerFragment(View v) { DatePickerDialogFragment datepicker = new DatePickerDialogFragment(); datepicker.setTargetFragment(this, DATEPICKER_FRAGMENT); datepicker.show(getFragmentManager(), "datePicker_professor"); } //SHOW THE TIMEPICKER DIALOG FRAGMENT public void showTimePickerFragment(View v) { TimePickerDialogFragment timepicker = new TimePickerDialogFragment(); timepicker.setTargetFragment(this, TIMEPICKER_FRAGMENT); timepicker.show(getFragmentManager().beginTransaction(), "timePicker_professor"); } //THIS METHOD RECIEVES THE DATA FROM THE DIALOG FRAGMENT @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { //MAKE SURE FRAGMENT CODES MATCH UP /* case DATEPICKER_FRAGMENT: Bundle date_bundle = data.getExtras(); int year = date_bundle.getInt("year"); int month = date_bundle.getInt("month"); int day = date_bundle.getInt("day"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { date_format = dateFormat.parse(year + "-" + (month + 1) + "-" + day); String actual_date = new SimpleDateFormat("EEEE, d MMMM").format(date_format); meeting_day.setText(actual_date); break; } catch (ParseException e) { e.printStackTrace(); } */ case TIMEPICKER_FRAGMENT: Bundle time_bundle = data.getExtras(); int hourOfday = time_bundle.getInt("hour"); int minute = time_bundle.getInt("minute"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); try { time_format = timeFormat.parse(hourOfday + ":" + minute); String actual_time = new SimpleDateFormat("HH : mm").format(time_format); // set the selected time_format to textview if (hourOfday > 12) { // activity_textview.setText(actual_time + " PM"); actual_time = actual_time + " PM"; } else { //activity_textview.setText(actual_time + " AM"); actual_time = actual_time + " AM"; } meeting_time.setText(actual_time); break; } catch (ParseException e) { e.printStackTrace(); } } } private class TimePickerListener implements View.OnClickListener { @Override public void onClick(View v) { showTimePickerFragment(v); } } private class DatePickerListener implements View.OnClickListener { @Override public void onClick(View v) { showDatePickerFragment(v); } } //REGSITER THE FRAGMENT AS A CONTRIBUTOR TO THE OPTIONS MENU @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); //REMOVE THE THREE DOT ICON FROM THE ACTIONBAR, SINCE IT IS NOT NEEDED FOR NOW MenuItem item = menu.findItem(R.id.action_settings); item.setVisible(false); //INFLATE CUSTOM ACTIONBAR ActionBar ab = getActivity().getActionBar(); ab.setDisplayShowTitleEnabled(false); ab.setDisplayShowCustomEnabled(true); ab.setDisplayShowHomeEnabled(false); View customActionView = getActivity().getLayoutInflater().inflate(R.layout.actionbar_custom_layout, null); ab.setCustomView(customActionView); //ADD A LISTENER TO THE VIEWS IN THE CUSTOM ACTION BAR LAYOUT acbar_cancel_button = (LinearLayout) customActionView.findViewById(R.id.custom_ab_cancel); acbar_save_button = (LinearLayout) customActionView.findViewById(R.id.custom_ab_done); acbar_cancel_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getActivity().onBackPressed(); } }); acbar_save_button.setOnClickListener(new SaveProfessorListener()); } private class SaveProfessorListener implements View.OnClickListener { @Override public void onClick(View view) { //ASSURE THAT NO EMPTY FIELDS ARE ENTERED if (add_prof_name_et.getText().toString().equals("")) { add_prof_name_et.post(new Runnable() { public void run() { add_prof_name_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_prof_name_et, 0); toast_empty_field("NAME FIELD CANNOT BE EMPTY"); } }); } else if (add_course_et.getText().toString().equals("")) { add_course_et.post(new Runnable() { public void run() { add_course_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_course_et, 0); toast_empty_field("COURSE FIELD CANNOT BE EMPTY"); } }); } else if (add_prof_name_et.getText().toString().equals("") && add_course_et.getText().toString().equals("")) { add_prof_name_et.post(new Runnable() { public void run() { add_prof_name_et.requestFocusFromTouch(); InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); lManager.showSoftInput(add_prof_name_et, 0); toast_empty_field("EMPTY FIELDS"); } }); } else { /* if (date_format == null || meeting_day.getText().toString().equalsIgnoreCase("Today")){ //GET CURRENT TIME getCurrentDate(); } */ if (time_format == null || meeting_time.getText().toString().equalsIgnoreCase("Now")){ //GET CURRENT TIME getCurrentTime(); } prof.setProfessor_name(add_prof_name_et.getText().toString()); prof.setProfessor_mail(add_prof_email_et.getText().toString()); prof.setProfessor_course(add_course_et.getText().toString()); //INSERT INTO PROFESSOR DATABASE long prof_id = professorDataSource.createProfessor(prof); //LOG THE NUMBER OF PROFESSORS Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + professorDataSource.getProfessorCount()); if (meeting_booked_with_prof) { //FOR DEBUGGING PURPOSE Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + meeting_booked_with_prof + " " + time_format + meeting_time.getText().toString()); Log.e("PROFESSOR COUNT", "PROFESSOR COUNT: " + meeting_booked_with_prof + " " + day_selected); //INSERT THE DATA INTO MEETING DB task.setTime(time_format); task.setDay(day_selected); task.setNote("Meeting with " + prof.getProfessor_name()); task.setTitle("Meeting"); task.setLocation(task_location.getText().toString()); long id = taskDataSource.createTask(task, prof); //LOG THE NUMBER OF MEETINGS Log.e("MEETING COUNT", "MEETING COUNT: " + taskDataSource.getTaskCount()); } //GO BACK TO THE PREVIOUS ACTIVITY getActivity().onBackPressed(); } } } }