Android Open Source - Gradulator Evaluation Activity






From Project

Back to project page Gradulator.

License

The source code is released under:

MIT License

If you think the Android project Gradulator 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

/*
The MIT License (MIT)//from  ww w.j a  v  a  2 s  . c  o m

Copyright (c) 2014 Gannon Lawlor

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gannon.gradulator;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import com.gannon.gradulator.database.EvaluationDataSource;
import com.gannon.gradulator.models.Course;
import com.gannon.gradulator.models.Evaluation;


public class EvaluationActivity extends Activity {

    private Course course;
    private Evaluation evaluation;
    private EvaluationDataSource evaluationDataSource;
    private boolean edit = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluation);

        getActionBar().setDisplayShowTitleEnabled(false);

        // Get extras from CourseActivity
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        course = (Course) extras.getSerializable("Course");
        evaluation = (Evaluation) extras.getSerializable("Evaluation");
        evaluationDataSource = new EvaluationDataSource(this);

        if (evaluation != null)
        {
            edit = true;

            EditText tName = (EditText) findViewById(R.id.text_evaluation_name);
            EditText tGrade = (EditText) findViewById(R.id.text_evaluation_grade);
            EditText tValue = (EditText) findViewById(R.id.text_evaluation_value);

            tName.setText(evaluation.getName());
            tGrade.setText(String.valueOf(evaluation.getGrade()));
            tValue.setText(String.valueOf(evaluation.getValue()));

        }

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    /** Called when the user clicks the Add Button **/
    public void addEvaluation() {
        EditText tName = (EditText) findViewById(R.id.text_evaluation_name);
        EditText tGrade = (EditText) findViewById(R.id.text_evaluation_grade);
        EditText tValue = (EditText) findViewById(R.id.text_evaluation_value);


        if( checkInput(tGrade.getText().toString()) && checkInput(tValue.getText().toString()) && checkName(tName.getText().toString())){
            String name = tName.getText().toString();
            double grade = Double.parseDouble(tGrade.getText().toString());
            double value = Double.parseDouble(tValue.getText().toString());
            long courseId = course.getId();
            if (edit) {
                evaluation.setName(name);
                evaluation.setGrade(grade);
                evaluation.setValue(value);

                evaluationDataSource.updateEvaluation(evaluation);
            }
            else {
                evaluationDataSource.createEvaluation(name, grade, value,courseId);
            }

            Intent addIntent = new Intent(EvaluationActivity.this, CourseActivity.class );
            addIntent.putExtra("Course", course);
            startActivity(addIntent);
            finish();
        }
        else {
            Context context = getApplicationContext();
            CharSequence text = "Opps! Your Grade and Weight have to be a number. Name can't be empty";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

    }

    public boolean checkInput(String input) {
        boolean answer = false;
        try {
            double s = Double.parseDouble(input);

            answer = true;
        } catch (NumberFormatException e) {

        }
        return answer;
    }

    private boolean checkName(String name) {
        if (name.length() > 0) {
            return true;
        }
        return false;
    }

    /** Called when the user clicks the cancel Button **/
    public void cancelEvaluation(View view) {
        Intent cancelIntent = new Intent(EvaluationActivity.this, CourseActivity.class );
        cancelIntent.putExtra("Course", course);
        startActivity(cancelIntent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.evaluation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_add_evaluation) {
            addEvaluation();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_evaluation, container, false);
            return rootView;
        }
    }
}




Java Source Code List

com.gannon.gradulator.ApplicationTest.java
com.gannon.gradulator.CourseActivity.java
com.gannon.gradulator.EvaluationActivity.java
com.gannon.gradulator.MainActivity.java
com.gannon.gradulator.adapters.CourseAdapter.java
com.gannon.gradulator.adapters.EvaluationAdapter.java
com.gannon.gradulator.database.CourseContract.java
com.gannon.gradulator.database.CourseDataSource.java
com.gannon.gradulator.database.CourseDbHelper.java
com.gannon.gradulator.database.EvaluationDataSource.java
com.gannon.gradulator.dialogfragments.AddCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteEvaluationDialogFragment.java
com.gannon.gradulator.models.Course.java
com.gannon.gradulator.models.Evaluation.java