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.students; /*ww w .j a v a2 s . c om*/ import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; 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.EditText; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import com.llenguatges.digitalcampus.MyApplication; import com.llenguatges.digitalcampus.R; import com.llenguatges.digitalcampus.database.StudentTable; import com.llenguatges.digitalcampus.objects.Student; public class NewStudentActivity extends Activity { private static int RESULT_LOAD_IMAGE = 1; private Student newStudent; private MyApplication myApp; /** * 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_student_search_photo: Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); break; case R.id.new_student_create: setStudent(); insertDB(); finish(); break; default: break; } } }; /** * Setter for specific attribute */ private void setStudent(){ EditText et = (EditText) findViewById(R.id.new_student_first_name); newStudent.setFirstName(et.getText().toString()); et = (EditText) findViewById(R.id.new_student_last_name); newStudent.setLastName(et.getText().toString()); DatePicker dp = (DatePicker) findViewById(R.id.new_student_bday); String bday = dp.getDayOfMonth()+"/"+dp.getMonth()+"/"+dp.getYear(); newStudent.setAge(bday); Spinner spinner = (Spinner) findViewById(R.id.new_student_degree_spinner); newStudent.setDescription(spinner.getSelectedItem().toString()); RadioGroup rg = (RadioGroup) findViewById(R.id.new_student_radio_sex); int selectedId = rg.getCheckedRadioButtonId(); RadioButton radioSexButton = (RadioButton) findViewById(selectedId); switch (radioSexButton.getId()) { case R.id.male: newStudent.setGender("M"); break; case R.id.female: newStudent.setGender("F"); break; default: break; } ImageView image = (ImageView) findViewById(R.id.new_student_image); Bitmap bmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); newStudent.setPhoto(bmap); } /** * Inserts information into the specific tables related to the Activity. */ protected void insertDB() { StudentTable st = new StudentTable(getApplicationContext()); long id = st.Insert(newStudent); newStudent.setId(id); myApp.addStudent(newStudent); } /** * 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.new_student_layout); myApp = (MyApplication) getApplication(); ActionBar actionBar = getActionBar(); actionBar.setSubtitle("New student"); actionBar.setDisplayHomeAsUpEnabled(true); setSpinner(); setButton(); newStudent = new Student(0, null, null, null, null, null, false, null); } /** * Inserts information into the specific tables related to the Activity. */ private void setButton() { Button bttImageLoader = (Button) findViewById(R.id.new_student_search_photo); bttImageLoader.setOnClickListener(onClick); Button bttCreates = (Button) findViewById(R.id.new_student_create); bttCreates.setOnClickListener(onClick); } /** * Set student's spinner view */ private void setSpinner() { Spinner spinner = (Spinner) findViewById(R.id.new_student_degree_spinner); 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); } /** * This hook is called whenever an item in your options menu is selected. */ public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } /** * Called when an activity you launched exits, * giving you the requestCode you started it with, * the resultCode it returned, and any additional data from it. */ protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); ImageView imgView = (ImageView) findViewById(R.id.new_student_image); imgView.setImageURI(selectedImage); } } } }