Android Open Source - ProjectStudio Update 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 av a 2 s.  c om
import android.app.ActionBar;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
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.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

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 model.Course;
import model.Professor;

/**
 * Created by desmond on 11/03/14.
 */
public class UpdateCourseFragment extends Fragment {

    private String previous_course_name;
    private String previous_course_semester;
    private String previous_course_prof;
    private String previous_course_room;
    private EditText update_course_name;
    private String previous_course_score;
    private String previous_course_status;
    private long course_id;
    private AutoCompleteTextView update_course_prof;
    private EditText update_course_room;
    private CheckBox update_course_pass_status;
    private EditText update_course_score;
    private TextView update_button;
    private TextView cancel_button;
    private LinearLayout bottom_layout;
    private DrawerLayout parent_drawer;
    private int course_is_passed = 0;
    private ProfessorDataSource prof_dataSource;
    private CourseDataSource course_dataSouce;
    private Course updated_course;
    private LinearLayout acbar_cancel_button;
    private LinearLayout acbar_save_button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.update_course_view, container, false);

        //OPEN THE DATABASE
        prof_dataSource = new ProfessorDataSource(getActivity());
        course_dataSouce = new CourseDataSource(getActivity());


        try {
            prof_dataSource.open();
            course_dataSouce.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 = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        parent_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        //GET DATA FROM BUNDLE
        Bundle bundle = this.getArguments();
        previous_course_name = bundle.getString("p_course_name");
        previous_course_room = bundle.getString("course_room");
        previous_course_semester = bundle.getString("course_semester");
        previous_course_prof = bundle.getString("professor_name");
        course_id = bundle.getLong("course_id");
        previous_course_score = bundle.getString("course_pass_mark");
        previous_course_status = bundle.getString("course_status");

        //INFLATE VIEWS HERE
        update_course_name = (EditText) view.findViewById(R.id.update_course_add_name);
        update_course_prof = (AutoCompleteTextView) view.findViewById(R.id.update_course_add_professor);
        update_course_room = (EditText) view.findViewById(R.id.update_course_add_room);
        update_course_score = (EditText) view.findViewById(R.id.update_course_add_passmark);
        //update_button = (TextView) view.findViewById(R.id.update_course_save_btn);
        //cancel_button = (TextView) view.findViewById(R.id.update_course_cancel_btn);
        update_course_pass_status = (CheckBox) view.findViewById(R.id.update_course_pass_status);
        bottom_layout = (LinearLayout) view.findViewById(R.id.update_course_add_linearview);
        bottom_layout.setVisibility(View.INVISIBLE);

        //SET HINTS TO PREVIOUS DATA
        update_course_name.setHint(previous_course_name);
        update_course_room.setHint(previous_course_room);
        update_course_prof.setHint(previous_course_prof);

        int score_status = Integer.parseInt(previous_course_status);
        if (score_status == 1) {
            update_course_pass_status.setChecked(true);
            update_course_score.setHint(previous_course_score + "");
            bottom_layout.setVisibility(View.VISIBLE);
        } else {
            update_course_pass_status.setChecked(false);
        }


        //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);
                update_course_prof.setAdapter(ad);
            }
        });

        //PUT LISTENERS HERE
        update_course_pass_status.setOnClickListener(new CheckPassStatusListener());
        //update_button.setOnClickListener(new UpdateCourseListener());
        //cancel_button.setOnClickListener(new CancelButtonListener());

        //CLOSE DATABASE
        prof_dataSource.close();
        course_dataSouce.close();
        return view;
    }

    private class CheckPassStatusListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (update_course_pass_status.isChecked()) {
                bottom_layout.setVisibility(View.VISIBLE);
                course_is_passed = 1;
            } else {
                bottom_layout.setVisibility(View.GONE);
            }
        }
    }

    private class UpdateCourseListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            //UPDATE THE DATE
            Professor professor = new Professor();
            professor.setProfessor_name(update_course_prof.getText().toString());
            updated_course = new Course();
            updated_course.setName(update_course_name.getText().toString());
            updated_course.setProfessor(professor);
            updated_course.setSemester(previous_course_semester);
            updated_course.setPassed(course_is_passed);
            updated_course.setId(course_id);
            updated_course.setRoom(update_course_room.getText().toString());


            if (update_course_prof.getText().toString().equals("")) {
                professor.setProfessor_name(update_course_prof.getHint().toString());
                updated_course.setProfessor(professor);
            }

            if (update_course_name.getText().toString().equals("")) {
                updated_course.setName(update_course_name.getHint().toString());
            }

            if (update_course_room.getText().toString().equals("")) {
                updated_course.setRoom(update_course_room.getHint().toString());
            }

            if (update_course_score.getText().equals("")) {
                updated_course.setPass_mark(update_course_score.getHint().toString());
            }

            if (update_course_pass_status.isChecked()) {
                updated_course.setPassed(1);
                updated_course.setPass_mark(update_course_score.getText().toString());
            }

            if (update_course_pass_status.isChecked() && update_course_score.getText().toString().equals("")) {
                updated_course.setPassed(1);
                updated_course.setPass_mark(update_course_score.getHint().toString());
            }

            //EXECUTE UPDATE
            course_dataSouce.updateCourse(updated_course);

            //RETURN TO PREVIOUS ACTIVITY
            getActivity().onBackPressed();

        }
    }

    private class CancelButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            //RETURN TO PREVIOUS FRAGMENT
            getActivity().onBackPressed();
        }
    }

    //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) {
        //REMOVE THE THREE DOT ICON FROM THE ACTIONBAR, SINCE IT IS NOT NEEDED FOR NOW
        super.onPrepareOptionsMenu(menu);
        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 CancelButtonListener());
        acbar_save_button.setOnClickListener(new UpdateCourseListener());
    }
}




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