Android Open Source - RaceTimer Add Event Fragment






From Project

Back to project page RaceTimer.

License

The source code is released under:

GNU General Public License

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

package com.liamfruzyna.android.racetimer.fragments;
// ww w .j  av  a 2  s. c o  m
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.liamfruzyna.android.racetimer.AppData;
import com.liamfruzyna.android.racetimer.objects.Meet;
import com.liamfruzyna.android.racetimer.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mail929 on 9/24/14.
 * A Fragment to create an event
 */
public class AddEventFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.add_event, container, false);

        //init view widgets
        final Meet meet = AppData.meets.get(getArguments().getInt("key"));
        ListView teamsList = (ListView) view.findViewById(R.id.runnerList);
        final EditText level = (EditText) view.findViewById(R.id.level);
        final EditText distance = (EditText) view.findViewById(R.id.distance);
        final EditText checkpoint = (EditText) view.findViewById(R.id.checkPoint);
        Button addCheck = (Button) view.findViewById(R.id.addCheckpoint);
        Button finish = (Button) view.findViewById(R.id.done);

        //create list of runner names
        final List<String> runnerNames = new ArrayList<String>();
        for(int i = 0; i < AppData.runners.size(); i++)
        {
            runnerNames.add(AppData.runners.get(i).name);
        }

        final List<Double> checkPoints = new ArrayList<Double>();
        final List<Integer> runners = new ArrayList<Integer>();

        //listview of teams
        teamsList.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, runnerNames)
        {
            public View getView(final int position, View convertView, ViewGroup parent)
            {
                //basic setup
                View view;
                if (convertView == null)
                {
                    LayoutInflater infl = (LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
                    convertView = infl.inflate(android.R.layout.simple_list_item_1, parent, false);
                }
                view = super.getView(position, convertView, parent);

                //init and add runner name to textview
                TextView tv = (TextView) view.findViewById(android.R.id.text1);
                tv.setText(runnerNames.get(position));
                return view;
            }
        });

        //listen for teams to be selected
        teamsList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                //if the button isn't clicked add the runner
                if(view.isEnabled())
                {
                    runners.add(i);
                }
                //otherwise remove the runner
                else
                {
                    for(int j = 0; j < runners.size(); j++)
                    {
                        if(runners.get(j) == i)
                        {
                            runners.remove(j);
                        }
                    }
                }
                //set the runner button to the opposite of what it was
                view.setEnabled(!view.isEnabled());
            }
        });

        //listen for button "add checkpoint to be added"
        addCheck.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //add a checkpoint and clear the textbox
                checkPoints.add(Double.parseDouble(checkpoint.getText().toString()));
                checkpoint.setText("");
            }
        });

        //listen for finish button to be pressed
        finish.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //save event
                AppData.meets.get(meet.id).addEvent(Double.parseDouble(distance.getText().toString()), checkPoints, level.getText().toString(), runners);

                //go to the event that was just created
                Fragment tf = new MeetFragment();
                Bundle args = new Bundle();
                args.putInt("key", AppData.meets.size()-1);
                tf.setArguments(args);
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.container, tf).commit();
            }
        });
        return view;
    }
}




Java Source Code List

com.liamfruzyna.android.racetimer.AppData.java
com.liamfruzyna.android.racetimer.ApplicationTest.java
com.liamfruzyna.android.racetimer.Fab.java
com.liamfruzyna.android.racetimer.RTActivity.java
com.liamfruzyna.android.racetimer.fragments.AddEventFragment.java
com.liamfruzyna.android.racetimer.fragments.AddMeetFragment.java
com.liamfruzyna.android.racetimer.fragments.AddTeamFragment.java
com.liamfruzyna.android.racetimer.fragments.EventFragment.java
com.liamfruzyna.android.racetimer.fragments.HomeFragment.java
com.liamfruzyna.android.racetimer.fragments.InRaceFragment.java
com.liamfruzyna.android.racetimer.fragments.MeetFragment.java
com.liamfruzyna.android.racetimer.fragments.RunnerFragment.java
com.liamfruzyna.android.racetimer.fragments.TeamFragment.java
com.liamfruzyna.android.racetimer.objects.Event.java
com.liamfruzyna.android.racetimer.objects.Meet.java
com.liamfruzyna.android.racetimer.objects.Runner.java
com.liamfruzyna.android.racetimer.objects.Team.java
com.liamfruzyna.android.racetimer.objects.Time.java