Android Open Source - Gradulator Course 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)//ww  w  .j av a2s. 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.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.gannon.gradulator.adapters.EvaluationAdapter;
import com.gannon.gradulator.database.CourseDataSource;
import com.gannon.gradulator.database.EvaluationDataSource;
import com.gannon.gradulator.dialogfragments.DeleteEvaluationDialogFragment;
import com.gannon.gradulator.models.Course;
import com.gannon.gradulator.models.Evaluation;

import java.sql.SQLException;
import java.util.List;


public class CourseActivity extends Activity {

    public Course course;
    private List<Evaluation> values;
    public EvaluationDataSource evaluationDataSource;
    public CourseDataSource courseDataSource;
    public EvaluationAdapter adapter;
    private Evaluation evaluation;
    public int listNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        TextView tCourseGrade = (TextView) findViewById(R.id.text_course_grade);
        TextView tCourseComplete = (TextView) findViewById(R.id.text_course_complete);

        tCourseGrade.setTypeface(null, Typeface.BOLD);
        tCourseComplete.setTypeface(null, Typeface.BOLD);

        evaluationDataSource = new EvaluationDataSource(this);
        courseDataSource = new CourseDataSource(this);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        course = (Course) getIntent().getSerializableExtra("Course");

        // Set the title in action bar to course name
        getActionBar().setTitle(course.getName());

        ListView myListView = (ListView) findViewById(R.id.evaluation_list);
        try {
            evaluationDataSource.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Get all evalueations
        values = evaluationDataSource.getAllEvaluationsByCourse(course.getId());
        calculateGrade();

        adapter = new EvaluationAdapter(this, values);
        myListView.setAdapter(adapter);

        adapter.notifyDataSetChanged();
        registerForContextMenu(myListView);

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

    public void deleteEvaluation(){
        DialogFragment newFragment = new DeleteEvaluationDialogFragment();
        newFragment.show(getFragmentManager(), "deletecourse");
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);
        MenuInflater inflater = getMenuInflater();
        menu.setHeaderTitle("Select an action");
        inflater.inflate(R.menu.course_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.button_delete:
                listNumber = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;
                deleteEvaluation();
                return true;
            case R.id.button_edit:
                listNumber = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;
                editEvaluation(listNumber);

                return true;
            case R.id.button_share:
                Evaluation shareEvaluation = adapter.getItem(listNumber);
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBody = "I got a " + shareEvaluation.getGrade() + "% on " + shareEvaluation.getName() + " in " + course.getName() + "! You can keep track of your grades too with #Gradeulator";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Look at Me!");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    public void editEvaluation(int pos) {
        evaluation = adapter.getItem(pos);
        sendData();
    }

    public void addEvaluation() {
        sendData();
    }

    public void sendData() {
        Intent intent = new Intent(CourseActivity.this, EvaluationActivity.class);
        Bundle extras = new Bundle();
        extras.putSerializable("Course", course);
        extras.putSerializable("Evaluation", evaluation);
        intent.putExtras(extras);
        startActivity(intent);
    }

    public void calculateGrade() {
        // Hold the total value complete
        double totalComplete = 0.0;
        // Hold the grade the user has eg. userGrade = (evaluationGrade/100)*evaluationValue
        double userGrade = 0.0;
        for(int i = 0; i < values.size(); i++) {
            userGrade = userGrade + (values.get(i).getGrade()/100.0)*values.get(i).getValue();
            totalComplete = totalComplete + values.get(i).getValue();
        }
        // Calculate courseGrade userGrade/totalComplete
        double courseGrade = (userGrade/totalComplete)*100;

        course.setGrade(courseGrade);
        course.setComplete(totalComplete);
        courseDataSource.updateCourse(course);

        // update the text for course grade and complete
        TextView tCourseGrade = (TextView) findViewById(R.id.text_course_grade);
        tCourseGrade.setText("Course Grade: " + course.roundGrade() + "%");

        TextView tCourseComplete = (TextView) findViewById(R.id.text_course_complete);
        tCourseComplete.setText("Course Complete: " + course.getComplete() + "%");
    }

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

    @Override
    public void onBackPressed() {

        Intent intent = new Intent(CourseActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
        return;
    }

    @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_addEvaluation) {
            addEvaluation();
            adapter.notifyDataSetChanged();
            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_course, 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