Android Open Source - ProjectStudio Add Course Fragment






From Project

Back to project page ProjectStudio.

License

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.

Java Source Code

package fragments;
//from  w ww .  j  a  v  a2  s  . c  o  m
import android.app.ActionBar;
import android.content.Context;
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.util.ArrayList;

import DB_Provider.CourseDataSource;
import DB_Provider.DB_ABSTRACTS;
import DB_Provider.ProfessorDataSource;
import dialog_fragments.DatePickerDialogFragment;
import model.Course;
import model.Professor;

/**
 * Created by desmond on 1/16/14.
 */
public class AddCourseFragment extends Fragment {

    private static final int DATEPICKER_FRAGMENT = 1;
    private EditText course_name;
    private EditText course_room;
    private AutoCompleteTextView course_professor;
    private Spinner course_semester_spinner;
    private CheckBox course_passed;
    private TextView cancel_btn;
    private TextView save_btn;
    private DrawerLayout parent_drawer;
    private EditText pass_mark;
    private int course_isPassed = 0;
    private LinearLayout passed_layout;
    private String selected_semester;
    private ProfessorDataSource prof_dataSource;
    private CourseDataSource course_dataSource;
    private Professor prof;
    private Course course;
    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.course_addview, container, false);

        prof_dataSource = new ProfessorDataSource(getActivity());
        course_dataSource = new CourseDataSource(getActivity());


        //OPEN THE DATABASE
        try {
            prof_dataSource.open();
            course_dataSource.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        prof = new Professor();
        course = new Course();

        //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 = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        parent_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        //INFLATE VIEWS HERE
        if (view != null) {
            course_semester_spinner = (Spinner) view.findViewById(R.id.course_add_semester_spinner);
            passed_layout = (LinearLayout) view.findViewById(R.id.course_add_linearview);
            passed_layout.setVisibility(View.INVISIBLE);
            course_passed = (CheckBox) view.findViewById(R.id.course_add_passed_cb);
            course_name = (EditText) view.findViewById(R.id.course_add_name);
            course_room = (EditText) view.findViewById(R.id.course_add_room);
            course_professor = (AutoCompleteTextView) view.findViewById(R.id.course_add_professor);
            //save_btn = (TextView) view.findViewById(R.id.course_add_save_btn);
            //cancel_btn = (TextView) view.findViewById(R.id.course_add_calcel_btn);
            pass_mark = (EditText) view.findViewById(R.id.course_add_passmark);
        }

        //ATTACH AUTOCOMPLETE LIST TO THE PROFESSOR TEXTFIELD
        final ArrayList<String> list = new ArrayList<String>();
        new android.os.Handler().post(new Runnable() {
            @Override
            public void run() {
                final Cursor cursor = prof_dataSource.getAllProfessors();
                if (cursor.moveToFirst()) {
                    do {
                        list.add(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.NAME_COLUMN)));
                    } while (cursor.moveToNext());
                }
                ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
                course_professor.setAdapter(ad);
            }
        });

        //PUT LISTENERS HERE
        course_passed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (course_passed.isChecked()) {
                    passed_layout.setVisibility(View.VISIBLE);
                    course_isPassed = 1;
                } else {
                    passed_layout.setVisibility(View.GONE);
                }
            }
        });

        //pass_date.setOnClickListener(new DatePickerListener());

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.semester_spinenr, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        course_semester_spinner.setAdapter(adapter);

        course_semester_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selected_semester = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        /*
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //ASSURE THAT NO EMPTY FIELDS ARE ENTERED
                if (course_name.getText().toString().equals("")) {
                    course_name.post(new Runnable() {
                        public void run() {
                            course_name.requestFocusFromTouch();
                            InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            lManager.showSoftInput(course_name, 0);
                            toast_empty_field("NAME FIELD CANNOT BE EMPTY");
                        }
                    });

                } else if (course_room.getText().toString().equals("")) {
                    course_room.post(new Runnable() {
                        public void run() {
                            course_room.requestFocusFromTouch();
                            InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            lManager.showSoftInput(course_room, 0);
                            toast_empty_field("COURSE ROOM FIELD CANNOT BE EMPTY");
                        }
                    });
                } else if (course_professor.getText().toString().equals("")) {
                    course_professor.post(new Runnable() {
                        public void run() {
                            course_professor.requestFocusFromTouch();
                            InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            lManager.showSoftInput(course_professor, 0);
                            toast_empty_field("EMPTY FIELDS");
                        }
                    });
                } else {

                    prof.setProfessor_name(course_professor.getText().toString());
                    course.setName(course_name.getText().toString());
                    course.setPass_mark(pass_mark.getText().toString());
                    course.setRoom(course_room.getText().toString());
                    course.setSemester(selected_semester);
                    course.setPassed(course_isPassed);

                    //INSERT INTO PROFESSOR DATABASE
                    long course_id = course_dataSource.createCourse(course, prof);

                    //LOG THE NUMBER OF PROFESSORS
                    Log.e("PROFESSOR COUNT", "COURSE COUNT: " + course_dataSource.getCourseCount());

                    //GO BACK TO THE PREVIOUS ACTIVITY
                    getActivity().onBackPressed();
                }
            }
        });
        */

        /*
        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });

        */

        prof_dataSource.close();
        course_dataSource.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();
    }

    //THIS METHOD RECIEVES THE DATA FROM THE DIALOG FRAGMENT
    /*
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        String time;
        String date;
        switch (requestCode) {
            //MAKE SURE FRAGMENT CODES MATCH UP
            case DATEPICKER_FRAGMENT:
                Bundle date_bundle = data.getExtras();
                date = date_bundle.getString("date");
                pass_date.setText(date);
                break;
        }
    } */

    // SHOW THE DATEPICKER DIALOG FRAGMENT
    public void showDatePickerFragment(View v) {
        DatePickerDialogFragment datepicker = new DatePickerDialogFragment();
        datepicker.setTargetFragment(this, DATEPICKER_FRAGMENT);
        datepicker.show(getFragmentManager(), "datePicker_professor");

    }

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

    private class SaveCourseListener implements View.OnClickListener {


        @Override
        public void onClick(View view) {
            //ASSURE THAT NO EMPTY FIELDS ARE ENTERED
            if (course_name.getText().toString().equals("")) {
                course_name.post(new Runnable() {
                    public void run() {
                        course_name.requestFocusFromTouch();
                        InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        lManager.showSoftInput(course_name, 0);
                        toast_empty_field("NAME FIELD CANNOT BE EMPTY");
                    }
                });

            } else if (course_room.getText().toString().equals("")) {
                course_room.post(new Runnable() {
                    public void run() {
                        course_room.requestFocusFromTouch();
                        InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        lManager.showSoftInput(course_room, 0);
                        toast_empty_field("COURSE ROOM FIELD CANNOT BE EMPTY");
                    }
                });
            } else if (course_professor.getText().toString().equals("")) {
                course_professor.post(new Runnable() {
                    public void run() {
                        course_professor.requestFocusFromTouch();
                        InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        lManager.showSoftInput(course_professor, 0);
                        toast_empty_field("EMPTY FIELDS");
                    }
                });
            } else {

                prof.setProfessor_name(course_professor.getText().toString());
                course.setName(course_name.getText().toString());
                course.setPass_mark(pass_mark.getText().toString());
                course.setRoom(course_room.getText().toString());
                course.setSemester(selected_semester);
                course.setPassed(course_isPassed);

                //INSERT INTO PROFESSOR DATABASE
                long course_id = course_dataSource.createCourse(course, prof);

                //LOG THE NUMBER OF PROFESSORS
                Log.e("PROFESSOR COUNT", "COURSE COUNT: " + course_dataSource.getCourseCount());

                //GO BACK TO THE PREVIOUS ACTIVITY
                getActivity().onBackPressed();
            }
        }
    }
}




Java Source Code List

DB_Provider.CourseDataSource.java
DB_Provider.DBManager.java
DB_Provider.DB_ABSTRACTS.java
DB_Provider.ProfessorDataSource.java
DB_Provider.TaskDataSource.java
Drawables.DrawableAlignedButton.java
FontPackage.TypefaceSpan.java
adapters.CourseCardAdapter.java
adapters.ExamsPassedCardAdapter.java
adapters.ExamsPendingCardAdapter.java
adapters.Exams_TabsPagerAdapter.java
adapters.HomeCardAdapter.java
adapters.MatesCardAdapter.java
adapters.NavDrawerListAdapter.java
adapters.ProfessorCardAdapter.java
adapters.TaskCardAdapter.java
adapters.TasksTabPagerAdapter.java
com.example.uniutilproject.BuildConfig.java
com.example.uniutilproject.UniUtil_MainActivity.java
days_fragments.FridayTaskFragment.java
days_fragments.MondayTaskFragment.java
days_fragments.SaturdayTaskFragment.java
days_fragments.SundayTaskFragment.java
days_fragments.ThursdayTaskFragment.java
days_fragments.TuesdayTaskFragment.java
days_fragments.WednesdayTaskFragment.java
dialog_fragments.DatePickerDialogFragment.java
dialog_fragments.MatesDialogFragment.java
dialog_fragments.ProfessorsDialogFragment.java
dialog_fragments.TimePickerDialogFragment.java
fragments.AddCourseFragment.java
fragments.AddProfFragment.java
fragments.AddTaskFragment.java
fragments.CoursesFragment.java
fragments.CursorLoaderListFragment.java
fragments.ExamsFragment.java
fragments.ExamsPassedFragment.java
fragments.ExamsPendingFragment.java
fragments.HomeFragment.java
fragments.MatesFragment.java
fragments.ProfessorsFragment.java
fragments.StudyPlanFragment.java
fragments.TasksFragment.java
fragments.UpdateCourseFragment.java
fragments.UpdateProfessorFragment.java
interfaces.NoticeDialogListener.java
interfaces.package-info.java
model.Course.java
model.Days.java
model.MyParcelable.java
model.NavDrawerItem.java
model.Professor.java
model.Task.java