Example #1
0
  /* (non-Javadoc)
   * @see android.app.Activity#onCreate()
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialize database
    try {
      db = DataFramework.getInstance();
      db.open(this, this.getPackageName());
      dbHelper = new DataBaseHelper(db);

    } catch (Exception ex) {
      Log.e(ex.getClass().getSimpleName(), ex.getMessage());
      error(ex.getMessage());
      ex.printStackTrace();
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newsubject_newclass);
    getWindow()
        .setLayout(LayoutParams.FILL_PARENT /* width */, LayoutParams.WRAP_CONTENT /* height */);

    classRoom = (EditText) findViewById(R.id.room);
    classStart = (TimePicker) findViewById(R.id.timePicker1);
    classStart.setIs24HourView(true);
    classEnd = (TimePicker) findViewById(R.id.timePicker2);
    classEnd.setIs24HourView(true);

    try {
      db.open(this, "com.albertoelias.agenda");
    } catch (Exception e) {
      e.printStackTrace();
    }

    daySpinner = (Spinner) findViewById(R.id.daySpinner);
    ArrayAdapter<CharSequence> adapter =
        ArrayAdapter.createFromResource(
            this, R.array.days_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    daySpinner.setAdapter(adapter);

    Button Cancel = (Button) findViewById(R.id.cancel);
    Button Submit = (Button) findViewById(R.id.submit);

    long mRowId =
        savedInstanceState != null ? savedInstanceState.getLong(DataFramework.KEY_ID) : -1;
    if (mRowId < 0) {
      Bundle extras = getIntent().getExtras();
      if (extras.containsKey("SubjectID")) {
        this.subjectId = extras.getString("SubjectID");
        currentEntity = new Entity("date");
      } else {
        mRowId = extras != null ? extras.getLong(DataFramework.KEY_ID) : -1;
        currentEntity = new Entity("date", mRowId);
        populateFields();
      }
    }

    Submit.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View view) {
            int daynumber = daySpinner.getSelectedItemPosition() + 2;
            if (daynumber == 8) {
              daynumber = 1;
            }
            currentEntity.setValue("day", daynumber);
            currentEntity.setValue("room", classRoom.getText().toString());
            currentEntity.setValue("starttimehour", classStart.getCurrentHour());
            currentEntity.setValue("starttimeminute", classStart.getCurrentMinute());
            currentEntity.setValue("endtimehour", classEnd.getCurrentHour());
            currentEntity.setValue("endtimeminute", classEnd.getCurrentMinute());
            // Para cuando funcione el pulsar en un elemento de la lista
            if (subjectId != null) {
              currentEntity.setValue("subject_id", subjectId);
            }
            currentEntity.save();
            setResult(RESULT_OK);
            finish();
          }
        });

    Cancel.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View cancel) {
            setResult(RESULT_CANCELED);
            finish();
          }
        });
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addassigndialog);

    getWindow()
        .setLayout(LayoutParams.FILL_PARENT /* width */, LayoutParams.WRAP_CONTENT /* height */);

    assignName = (EditText) findViewById(R.id.assignName);
    assignComment = (EditText) findViewById(R.id.assignComment);
    due = (DatePicker) findViewById(R.id.due);
    done = (CheckBox) findViewById(R.id.done);

    try {
      db.open(this, "com.albertoelias.aplusplusgenda");
    } catch (Exception e) {
      e.printStackTrace();
    }

    // Find out if the user is creating or editing
    mRowId = savedInstanceState != null ? savedInstanceState.getLong(DataFramework.KEY_ID) : -1;
    if (mRowId < 0) {
      // Get the day the user is viewing
      Bundle extras = getIntent().getExtras();
      Long milis = extras.getLong("todayCalendar");
      this.date.setTimeInMillis(milis);
      if (extras.containsKey("class_id")) {
        // Create a new assignment
        this.classId = extras.getLong("class_id");
        currentEntity = new Entity("assignment");
        // Set the due date to one day later.
        due.updateDate(
            date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH) + 1);
      } else {
        // Get the ID and open the assignment and populate the fields with the assignment's values
        mRowId = extras != null ? extras.getLong("assign_id") : -1;
        currentEntity = new Entity("assignment", mRowId);
        populateFields();
      }
    }

    // Mark the assignment as done
    done.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
              currentEntity.setValue("done", 1);
              isDone = true;
              currentEntity.save();
            } else {
              currentEntity.setValue("done", 0);
              currentEntity.save();
            }
          }
        });

    Button cancel = (Button) findViewById(R.id.cancel);
    cancel.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View cancel) {
            setResult(RESULT_CANCELED);
            finish();
          }
        });

    Button submit = (Button) findViewById(R.id.submit);
    submit.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View submit) {
            due.clearFocus();
            String assignTitle = assignName.getText().toString().trim();
            if (!assignTitle.equalsIgnoreCase("")) {
              int year = due.getYear();
              int month = due.getMonth();
              int day = due.getDayOfMonth();
              Calendar duedate = Calendar.getInstance();
              // Set everything to 0 because the only important part is the date
              duedate.set(year, month, day, 0, 0, 0);
              duedate.set(Calendar.MILLISECOND, 0);
              duedate.setTimeZone(TimeZone.getTimeZone("GMT"));
              // Check if the duedate is a valid date
              if (due.getYear() < date.get(Calendar.YEAR)
                  || due.getYear() > date.get(Calendar.YEAR) + 1
                  || duedate.getTimeInMillis() < date.getTimeInMillis()) {
                Toast.makeText(getApplicationContext(), R.string.notcorrectyear, Toast.LENGTH_LONG)
                    .show();
              } else {
                // Save everything
                currentEntity.setValue("title", assignName.getText().toString());
                currentEntity.setValue("comment", assignComment.getText().toString());
                currentEntity.setValue("due", duedate.getTimeInMillis());
                if (classId != null) {
                  currentEntity.setValue("setDate", date.getTimeInMillis());
                  currentEntity.setValue("class_id", classId);
                }
                currentEntity.save();
                setResult(RESULT_OK);
                finish();
              }
            } else {
              Toast.makeText(getApplicationContext(), R.string.noassignmessage, Toast.LENGTH_LONG)
                  .show();
            }
          }
        });
  }