Back to project page digitalcampus.
The source code is released under:
MIT License
If you think the Android project digitalcampus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.llenguatges.digitalcampus.exams; /*w w w .j av a 2s.co m*/ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.net.ParseException; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.Spinner; import android.widget.TimePicker; import com.llenguatges.digitalcampus.R; import com.llenguatges.digitalcampus.adapters.SpinnerAdapter; import com.llenguatges.digitalcampus.database.ExamTable; import com.llenguatges.digitalcampus.database.SubjectTable; import com.llenguatges.digitalcampus.objects.Exam; public class NewExamActivity extends Activity{ private Long id; private Exam exam; private ExamTable examTable; private Button save; /** * Interface definition for a callback to be invoked when a view is clicked. */ private OnClickListener onClick = new OnClickListener() { /** * Called when a view has been clicked. */ public void onClick(View v) { switch (v.getId()) { case R.id.new_exam_save: setExam(); if(id == 0){ examTable.Insert(exam); }else{ examTable.UpdateTableById(exam); } finish(); break; default: break; } } }; /** * Called when the activity is first created. * This is where you all of static set up: customize ActionBar. * This method also provides you with a Bundle containing the * activity's previously frozen state, if there was one. */ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_exam); ActionBar actionBar = getActionBar(); actionBar.setSubtitle("New/Edit exam"); actionBar.setDisplayHomeAsUpEnabled(true); Bundle extras = getIntent().getExtras(); TimePicker tp = (TimePicker) findViewById(R.id.new_exam_time); tp.setIs24HourView(true); save = (Button) findViewById(R.id.new_exam_save); save.setOnClickListener(onClick); id = extras.getLong("id"); exam = new Exam(0, 0, null, null, null); examTable = new ExamTable(getApplicationContext()); if(id == 0){ newExamLoadView(); }else{ try { updateExamloadView(); } catch (java.text.ParseException e) { e.printStackTrace(); } } } private void newExamLoadView() { setSpinners(); } @SuppressLint("SimpleDateFormat") private void updateExamloadView() throws java.text.ParseException { exam = examTable.GetExamById(id); DatePicker dp = (DatePicker) findViewById(R.id.new_exam_date); String[] date = exam.getDate().split("/"); int day = Integer.parseInt(date[0].toString()); int month = Integer.parseInt(date[1].toString()); int year = Integer.parseInt(date[2].toString()); dp.updateDate(year, month, day); TimePicker tp = (TimePicker) findViewById(R.id.new_exam_time); tp.setIs24HourView(true); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm"); Date time = null; try { time = sdf.parse(exam.getTime()); } catch (ParseException e) { } Calendar c = Calendar.getInstance(); c.setTime(time); tp.setCurrentHour(c.get(Calendar.HOUR_OF_DAY)); tp.setCurrentMinute(c.get(Calendar.MINUTE)); setSpinners(); } /** * Set exam values */ private void setExam() { DatePicker dp = (DatePicker) findViewById(R.id.new_exam_date); String date = dp.getDayOfMonth() + "/" + dp.getMonth() + "/" + dp.getYear(); exam.setDate(date); TimePicker tp = (TimePicker) findViewById(R.id.new_exam_time); tp.clearFocus(); String time; if (tp.getCurrentMinute().toString().length() < 2) { time = tp.getCurrentHour().toString() + ":0" + tp.getCurrentMinute().toString(); } else { time = tp.getCurrentHour().toString() + ":" + tp.getCurrentMinute().toString(); } exam.setTime(time); Spinner spinner = (Spinner) findViewById(R.id.new_exam_classroom); exam.setClassroom(spinner.getSelectedItem().toString()); spinner = (Spinner) findViewById(R.id.new_exam_subject); SubjectTable st = new SubjectTable(getApplicationContext()); exam.setSubject(st.GetIdByName(spinner.getSelectedItem().toString())); examTable.UpdateTableById(exam); } /** * Set exam's spinners */ private void setSpinners() { Spinner spinner = (Spinner) findViewById(R.id.new_exam_degree); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.degree_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner = (Spinner) findViewById(R.id.new_exam_classroom); adapter = ArrayAdapter.createFromResource(this, R.array.classroom_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); List<String> subjectList = new ArrayList<String>(); SubjectTable st = new SubjectTable(getApplicationContext()); subjectList = st.GetNamesFromTable(); SpinnerAdapter spiAdapter = new SpinnerAdapter(this, subjectList); spinner = (Spinner) findViewById(R.id.new_exam_subject); spinner.setAdapter(spiAdapter); } /** * This hook is called whenever an item in your options menu is selected. */ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } }