com.fitme.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.fitme.MainActivity.java

Source

/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fitme;

import android.app.ActionBar;
import android.app.Activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.fitme.db.helper.DbHelper;

import com.fitme.adapter.AppSectionsPagerAdapter;
import com.fitme.adapter.ProgramListAdapter;
import com.fitme.adapter.TrainingsListAdapter;
import com.fitme.db.entry.Exercise;
import com.fitme.db.entry.Program;
import com.fitme.db.entry.Training;
import com.fitme.db.dao.ActiveProgramDAO;
import com.fitme.db.dao.ExerciseDAO;
import com.fitme.db.dao.ProgramDAO;
import com.fitme.db.dao.TrainingDAO;
import com.fitme.gui.fragment.DummySectionFragment;
import com.fitme.gui.fragment.TrainingsSectionFragment;
import com.fitme.gui.fragment.WorkoutsSectionFragment;
import com.fitme.gui.row.ExerciseRow;
import com.fitme.gui.row.ProgramRow;
import com.fitme.gui.row.TrainingRow;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    private ViewPager mViewPager;
    private String activeProgram = "";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Init db scheme
        DbHelper dbh = new DbHelper(this);
        dbh.createDataBase();
        ActiveProgramDAO apd = new ActiveProgramDAO(this);
        activeProgram = apd.getActiveProgramName();
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(this);
        // Refresh trainings section data
        TrainingsSectionFragment tsf = (TrainingsSectionFragment) mAppSectionsPagerAdapter
                .getItem(AppSectionsPagerAdapter.SECTION_TRAININGS);
        tsf.onNewProgramSelected();

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                if (actionBar != null)
                    actionBar.setSelectedNavigationItem(position);
            }
        });

        if (actionBar != null) {
            // Specify that the Home/Up button should not be enabled, since there is no hierarchical
            // parent.
            actionBar.setHomeButtonEnabled(false);
            // Specify that we will be displaying tabs in the action bar.
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // For each of the sections in the app, add a tab to the action bar.
            for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
                // Create a tab with text corresponding to the page title defined by the adapter.
                // Also specify this Activity object, which implements the TabListener interface, as the
                // listener for when this tab is selected.
                actionBar.addTab(
                        actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
            }

            actionBar.getTabAt(WorkoutsSectionFragment.WORKOUTS_SECTION_ID).setIcon(R.drawable.ic_section_workouts);
            actionBar.getTabAt(TrainingsSectionFragment.TRAININGS_SECTION_ID)
                    .setIcon(R.drawable.ic_section_trainings);
            actionBar.getTabAt(DummySectionFragment.DUMMY_SECTION_ID).setIcon(R.drawable.ic_section_charts);
            // Additional fake tab for the future development
            actionBar.getTabAt(DummySectionFragment.DUMMY_SECTION_ID + 1).setIcon(R.drawable.ic_section_photo);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options, menu);
        MenuItem mi = menu.findItem(R.id.menu_spinner_active_program);
        final TextView tv = (TextView) mi.getActionView();
        ActiveProgramDAO apd = new ActiveProgramDAO(MainActivity.this);
        activeProgram = apd.getActiveProgramName();
        tv.setText(activeProgram);

        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.setTitle(getText(R.string.title_select_prog_dialog));
                View programView = getLayoutInflater().inflate(R.layout.programs_list, null);
                ExpandableListView elv = (ExpandableListView) programView.findViewById(R.id.list_programs);
                final ProgramListAdapter pda = new ProgramListAdapter(MainActivity.this);
                elv.setAdapter(pda);
                // Setting listener for Add Program button
                Button addProgram = (Button) programView.findViewById(R.id.button_add_program);
                addProgram.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddProgramRequested(pda);
                    }
                });
                elv.setGroupIndicator(null);
                elv.setOnGroupClickListener(null);
                elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition,
                            int childPosition, long l) {
                        pda.setProgramSelected(groupPosition, childPosition);
                        onNewProgramSelected(pda.getSelectedProgramName());
                        // Refresh trainings section list
                        TrainingsSectionFragment tsf = (TrainingsSectionFragment) mAppSectionsPagerAdapter
                                .getItem(AppSectionsPagerAdapter.SECTION_TRAININGS);
                        tsf.onNewProgramSelected();
                        dialog.dismiss();
                        return false;
                    }
                });
                dialog.setContentView(programView);
                dialog.show();
                dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT);
            }
        });

        return true;
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A fragment representing a exercises managment section of the app
     */

    private void onAddProgramRequested(final ProgramListAdapter pda) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.title_add_program_dialog);
        builder.setView(this.getLayoutInflater().inflate(R.layout.dialog_add_program, null));
        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);
        final AlertDialog programDialog = builder.create();
        programDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                Button positive = programDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Program p = new Program();
                        EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
                        EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
                        String programName = etName.getText().toString();
                        String programDesc = etDesc.getText().toString();
                        // Check if form filled correct
                        // Check if program name is empty
                        if (programName.isEmpty()) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_empty),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // Check if program already exists
                        ProgramDAO pd = new ProgramDAO(MainActivity.this);
                        if (pd.getProgramByName(programName).getId() != ProgramDAO.ID_PROGRAM_NOT_FOUND) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_exists),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // If everything is ok - then add new program
                        p.setName(programName);
                        p.setDescription(programDesc);
                        pd.createProgram(p);
                        Toast.makeText(MainActivity.this, getText(R.string.toast_add_program_ok),
                                Toast.LENGTH_SHORT).show();
                        pda.addProgram(p);
                        programDialog.dismiss();
                    }
                });
            }
        });
        programDialog.show();

    }

    public void onProgramEditRequested(final ProgramRow oldProg, final ProgramListAdapter pda) {
        final Activity activity = MainActivity.this;
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(getString(R.string.title_upd_program_dialog));
        builder.setView(getLayoutInflater().inflate(R.layout.dialog_add_program, null));
        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);
        final AlertDialog programDialog = builder.create();
        programDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                Button positive = programDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
                        EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
                        String programName = etName.getText().toString();
                        String programDesc = etDesc.getText().toString();
                        // Validating form fields
                        // Check if program name changed
                        if (!programName.equals(oldProg.getName())) {
                            // If chanded - check it
                            // Check if empty
                            if (programName.isEmpty()) {
                                Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_empty),
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                            // Check if program already exists
                            ProgramDAO pd = new ProgramDAO(MainActivity.this);
                            if (pd.getProgramByName(programName).getId() != ProgramDAO.ID_PROGRAM_NOT_FOUND) {
                                Toast.makeText(MainActivity.this, getString(R.string.toast_err_program_exists),
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                        } else {
                            // if program name stays the same - check description
                            // if description is the same - nothing happens
                            if (programDesc.equals(oldProg.getDescription())) {
                                programDialog.dismiss();
                                return;
                            }
                        }

                        // if everything is ok - update program record
                        Program p = new Program();
                        p.setName(programName);
                        p.setDescription(programDesc);
                        ProgramDAO pd = new ProgramDAO(activity);
                        pd.updateByName(oldProg.getName(), p);
                        Toast.makeText(activity, getText(R.string.toast_upd_program_ok), Toast.LENGTH_SHORT).show();
                        pda.updateProgram(oldProg.getName(), p);
                        programDialog.dismiss();
                    }
                });

            }
        });
        programDialog.show();
        // Initiate data with old program entry
        EditText etName = (EditText) programDialog.findViewById(R.id.edittext_program_name);
        EditText etDesc = (EditText) programDialog.findViewById(R.id.edittext_program_desc);
        etName.setText(oldProg.getName());
        etDesc.setText(oldProg.getDescription());
    }

    public void onProgramRemoveRequested(final ProgramRow oldProgram, final ProgramListAdapter pda) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.title_del_program_dialog));
        builder.setMessage(getString(R.string.message_del_program_dialog));
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                ProgramDAO pd = new ProgramDAO(MainActivity.this);
                long programId = pd.getProgramByName(oldProgram.getName()).getId();
                if (getActiveProgramId() == programId) {
                    // In this case we set first default programm as active
                    ProgramRow pr = (ProgramRow) pda.getChild(ProgramListAdapter.GROUP_DEFAULT_PROGRAMS, 0);
                    ActiveProgramDAO apd = new ActiveProgramDAO(MainActivity.this);
                    apd.setActiveProgram(pr.getName());
                    pda.setProgramSelected(ProgramListAdapter.GROUP_DEFAULT_PROGRAMS, 0);
                    onNewProgramSelected(pr.getName());

                }
                pd.deleteProgramByName(oldProgram.getName());
                pda.deleteProgram(oldProgram.getName());
                Toast.makeText(MainActivity.this, getString(R.string.toast_del_program_ok), Toast.LENGTH_SHORT)
                        .show();
                dialog.dismiss();

            }
        });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog delProgDialog = builder.create();
        delProgDialog.show();
    }

    public long getActiveProgramId() {
        ProgramDAO pd = new ProgramDAO(this);
        return pd.getProgramByName(activeProgram).getId();
    }

    public void onTrainingCreationRequested(final TrainingsListAdapter tla) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.title_add_training_dialog);
        View addTrainDialogView = getLayoutInflater().inflate(R.layout.dialog_add_training, null);
        builder.setView(addTrainDialogView);

        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);

        final AlertDialog trainingDialog = builder.create();

        trainingDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                Button positive = trainingDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // handle OK

                        EditText etName = (EditText) trainingDialog.findViewById(R.id.edittext_training_name);
                        String trainingName = etName.getText().toString();

                        // Check if form filled correct
                        // Check if training name is empty
                        if (trainingName.isEmpty()) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_empty),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // Check if training already exists
                        long programId = getActiveProgramId();
                        TrainingDAO td = new TrainingDAO(MainActivity.this);
                        if (td.getTrainingByName(programId, trainingName)
                                .getId() != TrainingDAO.ID_TRAINING_NOT_FOUND) {
                            Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_exists),
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        // If everything is ok - then add new program
                        Training t = new Training();
                        t.setName(trainingName);
                        t.setProgramId(programId);
                        //Insert entry to db
                        td.createTraining(t);
                        Toast.makeText(MainActivity.this, getText(R.string.toast_add_training_ok),
                                Toast.LENGTH_SHORT).show();
                        tla.addTraining(t);
                        trainingDialog.dismiss();
                    }
                });

                Button negative = trainingDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
                negative.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        trainingDialog.dismiss();

                    }
                });

            }
        });
        trainingDialog.show();
    }

    public void onExerciseCreationRequested(final String trainName, final TrainingsListAdapter tla) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.title_add_exercise_dialog);
        View view = getLayoutInflater().inflate(R.layout.dialog_add_exercise, null);
        builder.setView(view);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);
        final AlertDialog dialogEx = builder.create();
        dialogEx.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                Button positive = dialogEx.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Spinner spinMuscleGroup = (Spinner) dialogEx.findViewById(R.id.spinner_select_mg_ex);
                        Spinner spinExName = (Spinner) dialogEx.findViewById(R.id.spinner_select_ex);
                        NumberPicker numpickRepeats = (NumberPicker) dialogEx
                                .findViewById(R.id.numberpicker_repeats_ex);
                        //Since we decide to have all spinners pre-populated no need to check data
                        Exercise e = new Exercise();
                        e.setName(spinExName.getSelectedItem().toString());
                        e.setMuscleGroup(spinMuscleGroup.getSelectedItem().toString());
                        e.setRepeats(numpickRepeats.getValue());
                        //Identifying program and training
                        long programId = MainActivity.this.getActiveProgramId();
                        TrainingDAO td = new TrainingDAO(MainActivity.this);
                        long trId = td.getTrainingByName(programId, trainName).getId();
                        e.setTrainId(trId);

                        //Insert new entry to db
                        ExerciseDAO ed = new ExerciseDAO(MainActivity.this);
                        long exId = ed.createExercise(e).getId();
                        e.setId(exId);
                        Toast.makeText(MainActivity.this, getText(R.string.toast_add_exercise_ok),
                                Toast.LENGTH_SHORT).show();
                        tla.addExercise(e, trainName);
                        dialogEx.dismiss();
                    }
                });

            }
        });
        dialogEx.show();
        // Populate dialog fields
        NumberPicker np = (NumberPicker) dialogEx.findViewById(R.id.numberpicker_repeats_ex);
        final Spinner spinMuscleGroup = (Spinner) dialogEx.findViewById(R.id.spinner_select_mg_ex);
        final Spinner spinExName = (Spinner) dialogEx.findViewById(R.id.spinner_select_ex);
        np.setMaxValue(Exercise.MAX_REPEATS);
        np.setMinValue(Exercise.MIN_REPEATS);

        //Filling muscle groups spinner
        String[] muscleGroups = getResources().getStringArray(R.array.muscle_groups);

        ArrayAdapter<String> mgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                muscleGroups);
        spinMuscleGroup.setAdapter(mgAdapter);

        spinMuscleGroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String arrayName = ("exercises_" + spinMuscleGroup.getSelectedItem().toString()).toLowerCase();
                int id = getResources().getIdentifier(arrayName, "array", MainActivity.this.getPackageName());
                String[] exercises = getResources().getStringArray(id);
                ArrayAdapter<String> exAdapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item, exercises);
                spinExName.setAdapter(exAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spinMuscleGroup.setSelection(1);

    }

    public void onExerciseRemoveRequested(final ExerciseRow oldEx, final TrainingsListAdapter tla) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.title_del_exercise_dialog));
        builder.setMessage(getString(R.string.message_del_exercise_dialog));
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ExerciseDAO ed = new ExerciseDAO(MainActivity.this);
                long trainId = ed.getExerciseById(oldEx.getId()).getTrainId();
                TrainingDAO td = new TrainingDAO(MainActivity.this);
                String trainName = td.getTrainingById(trainId).getName();
                ed.deleteExerciseById(oldEx.getId());
                tla.deleteExercise(trainName, oldEx);
                Toast.makeText(MainActivity.this, getString(R.string.toast_del_exercise_ok), Toast.LENGTH_SHORT)
                        .show();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog delProgDialog = builder.create();
        delProgDialog.show();
    }

    public void onTrainingEditRequested(final TrainingRow oldTrain, final TrainingsListAdapter tla) {
        final Activity activity = MainActivity.this;
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(getString(R.string.title_upd_training_dialog));
        builder.setView(getLayoutInflater().inflate(R.layout.dialog_add_training, null));
        builder.setPositiveButton(android.R.string.ok, null);
        builder.setNegativeButton(android.R.string.cancel, null);
        final AlertDialog trainDialog = builder.create();
        trainDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button positive = trainDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                positive.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText etName = (EditText) trainDialog.findViewById(R.id.edittext_training_name);
                        String trainName = etName.getText().toString();
                        // Validating form fields
                        // Check if program name changed
                        if (!trainName.equals(oldTrain.getName())) {
                            // If chanded - check it
                            // Check if empty
                            if (trainName.isEmpty()) {
                                Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_empty),
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                            // Check if training already exists

                            TrainingDAO td = new TrainingDAO(MainActivity.this);
                            long programId = getActiveProgramId();
                            if (td.getTrainingByName(programId, trainName)
                                    .getId() != TrainingDAO.ID_TRAINING_NOT_FOUND) {
                                Toast.makeText(MainActivity.this, getString(R.string.toast_err_training_exists),
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                        } else {
                            // if program name stays the same - do nothing
                            trainDialog.dismiss();
                            return;

                        }

                        // if everything is ok - update program record
                        TrainingDAO td = new TrainingDAO(activity);
                        Training t = new Training();
                        long programId = getActiveProgramId();
                        t.setName(trainName);
                        t.setProgramId(programId);
                        t.setId(oldTrain.getId());
                        td.updateTraining(t);
                        Toast.makeText(activity, getText(R.string.toast_upd_training_ok), Toast.LENGTH_SHORT)
                                .show();
                        tla.updateTraining(t);
                        trainDialog.dismiss();
                    }
                });

            }
        });
        trainDialog.show();
        // Initiate data with old program entry
        EditText etName = (EditText) trainDialog.findViewById(R.id.edittext_training_name);
        etName.setText(oldTrain.getName());
    }

    public void onTrainingRemoveRequested(final TrainingRow oldTrain, final TrainingsListAdapter tla) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.title_del_training_dialog));
        builder.setMessage(getString(R.string.message_del_train_dialog));
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                TrainingDAO td = new TrainingDAO(MainActivity.this);
                td.deleteTrainingById(oldTrain.getId());
                tla.deleteTraining(oldTrain.getId());
                Toast.makeText(MainActivity.this, getString(R.string.toast_del_training_ok), Toast.LENGTH_SHORT)
                        .show();
                dialog.dismiss();

            }
        });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog delTrainDialog = builder.create();
        delTrainDialog.show();
    }

    public void onNewProgramSelected(String newProgramName) {
        activeProgram = newProgramName;
        ActiveProgramDAO apd = new ActiveProgramDAO(MainActivity.this);
        apd.setActiveProgram(activeProgram);
        TextView tv = (TextView) findViewById(R.id.menu_spinner_active_program);
        tv.setText(newProgramName);
        mAppSectionsPagerAdapter.onNewProgramSelected();
    }
}