Android Open Source - iEtueri New Course Fragment






From Project

Back to project page iEtueri.

License

The source code is released under:

GNU General Public License

If you think the Android project iEtueri 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 courses;
/*from   www.ja  v a2 s. com*/
import java.util.Calendar;

import navigationdrawer.MainActivity;
import navigationdrawer.NavigationDrawerController;
import schedule.Event;
import utility.DatabaseContract;
import utility.DatabaseHelper;
import utility.DatePickerDialogFragment;
import utility.OnBackPressed;
import utility.OnClickButtonXml;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.javils.ietueri.R;

public class NewCourseFragment extends Fragment implements OnClickButtonXml, OnBackPressed {

  private EditText nameCourse;
  private Button fromDate;
  private Button toDate;
  private DatabaseHelper dbHelper;
  private SQLiteDatabase db;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_new_course, container, false);
    dbHelper = new DatabaseHelper(getActivity());
    db = dbHelper.getWritableDatabase();

    Calendar calendar = Calendar.getInstance();

    /** Get the instance of the views */
    nameCourse = (EditText) view.findViewById(R.id.newcourse_course_name);
    fromDate = (Button) view.findViewById(R.id.newcourse_course_init);
    toDate = (Button) view.findViewById(R.id.newcourse_course_end);

    String[] sDate = Event.getDate(calendar.getTimeInMillis()).split("-");
    int year = Integer.valueOf(sDate[0]);
    int month = Integer.valueOf(sDate[1]);
    int day = Integer.valueOf(sDate[2]);
    fromDate.setHint(day + "/" + month + "/" + year);
    toDate.setHint(day + "/" + month + "/" + year);
    return view;
  }

  public void showDatePickerDialog(View view) {
    DatePickerDialogFragment dateFragment = new DatePickerDialogFragment();
    dateFragment.setHandler((Button) view);
    dateFragment.show(getFragmentManager(), "newCourseDatePicker");
  }

  /** Cancel the new course and back to Course View */
  private void cancelNewCourse() {
    FragmentManager fragmentManager = getFragmentManager();
    Fragment newFragment = NavigationDrawerController
        .newInstance(NavigationDrawerController.SECTION_NUMBER_COURSES);
    if (newFragment instanceof OnClickButtonXml)
      MainActivity.setOnClickFragment(newFragment);
    MainActivity.setCurrentFragment(newFragment);
    fragmentManager.beginTransaction().replace(R.id.navigation_drawer_container, newFragment).commit();

    /** Close the database */
    db.close();
    dbHelper.close();

  }

  private void addNewCourse() {
    if (nameCourse.getText().toString().isEmpty())
      Toast.makeText(getActivity(), "No hay nombre de curso. Por favor rellenalo.", Toast.LENGTH_SHORT).show();
    else {
      /** Fill the database with the data */
      ContentValues values = new ContentValues();
      values.put(DatabaseContract.Courses._ID, DatabaseHelper.getNextId(db, DatabaseContract.Courses.TABLE_NAME));
      values.put(DatabaseContract.Courses.COLUMN_NAME_COURSE_NAME, nameCourse.getText().toString());
      values.put(DatabaseContract.Courses.COLUMN_NAME_NUMBER_OF_SUBJECTS, 0);
      values.put(DatabaseContract.Courses.COLUMN_NAME_AVERAGE, 0);
      values.put(DatabaseContract.Courses.COLUMN_NAME_INIT_DATE, fromDate.getHint().toString());
      values.put(DatabaseContract.Courses.COLUMN_NAME_END_DATE, toDate.getHint().toString());
      values.put(DatabaseContract.Courses.COLUMN_NAME_SUBJECTS_IDS, "");

      db.insert(DatabaseContract.Courses.TABLE_NAME, null, values);

      /** Back to Courses view */
      FragmentManager fragmentManager = getFragmentManager();
      Fragment newFragment = NavigationDrawerController
          .newInstance(NavigationDrawerController.SECTION_NUMBER_COURSES);
      if (newFragment instanceof OnClickButtonXml)
        MainActivity.setOnClickFragment(newFragment);
      MainActivity.setCurrentFragment(newFragment);
      fragmentManager.beginTransaction().replace(R.id.navigation_drawer_container, newFragment).commit();

      /** Close the database */
      db.close();
      dbHelper.close();
    }
  }

  @Override
  public void onClickXml(View view) {

    switch (view.getId()) {
    case R.id.newcourse_course_init:
    case R.id.newcourse_course_end:
      showDatePickerDialog(view);
      break;
    case R.id.neworcancel_actionbar_acept:
      addNewCourse();
      break;
    case R.id.neworcancel_actionbar_cancel:
      cancelNewCourse();
      break;
    }
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(getArguments()
        .getInt(NavigationDrawerController.ARG_SECTION_NUMBER));
  }

  @Override
  public void onBackPressed() {
    cancelNewCourse();

  }

}




Java Source Code List

courses.CourseDetailFragment.java
courses.Course.java
courses.CoursesAdapter.java
courses.CoursesFragment.java
courses.NewCourseFragment.java
exams.Exam.java
exams.ExamsAdapter.java
exams.ExamsFragment.java
exams.NewExamFragment.java
homework.HomeworkAdapter.java
homework.HomeworkFragment.java
homework.Homework.java
homework.NewHomeworkFragment.java
navigationdrawer.DrawerListAdapter.java
navigationdrawer.DrawerList.java
navigationdrawer.MainActivity.java
navigationdrawer.NavigationDrawerCallbacks.java
navigationdrawer.NavigationDrawerController.java
navigationdrawer.NavigationDrawerFragment.java
navigationdrawer.OpenBrowser.java
schedule.CalendarManager.java
schedule.Event.java
schedule.EventsManager.java
schedule.FindEvents.java
schedule.RefreshScheduleEventsData.java
schedule.ScheduleFragment.java
schedule.ScheduleTodayAdapter.java
subject.DetailSubjectFragment.java
subject.NewSubjectFragment.java
subject.SubjectFragment.java
subject.SubjectListAdapter.java
subject.Subject.java
today.NewEventRepetitionDialogFragment.java
today.NewEventTodayFragment.java
today.TodayFragment.java
utility.DatabaseContract.java
utility.DatabaseHelper.java
utility.DatePickerDialogFragment.java
utility.OnBackPressed.java
utility.OnClickButtonXml.java
utility.PriorityDialogFragment.java
utility.TimePickerDialogFragment.java