Android Open Source - RaceTimer Add Team 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;
/*from   w w  w.  jav  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.R;

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

/**
 * Created by mail929 on 9/19/14.
 * A Fragment to create a team (and runners)
 */
public class AddTeamFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.add_team, container, false);
        //init view widgets
        final EditText name = (EditText) view.findViewById(R.id.name);
        final EditText shortName = (EditText) view.findViewById(R.id.shortName);
        TextView runners = (TextView) view.findViewById(R.id.runners);
        Button newRunner = (Button) view.findViewById(R.id.newRunner);
        final EditText newName = (EditText) view.findViewById(R.id.newName);
        Button finish = (Button) view.findViewById(R.id.done);
        ListView currentRunners = (ListView) view.findViewById(R.id.currentRunners);

        final List<Integer> runnerIds = new ArrayList<Integer>();

        //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);
        }

        //listviews of runners
        currentRunners.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 name to textview
                TextView tv = (TextView) view.findViewById(android.R.id.text1);
                tv.setText(runnerNames.get(position));
                return view;
            }
        });

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

        //listen for new runner button to be pressed
        newRunner.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //add the runner name from the textbox
                AppData.addRunner(newName.getText().toString());
                AppData.runners.get(AppData.runners.size()-1).teams.add(AppData.teams.size());
                runnerIds.add(AppData.runners.size() - 1);

                //clear the edittext
                newName.setText("");
            }
        });

        //listen for the finish button to be pressed
        finish.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //save team data
                AppData.addTeam(name.getText().toString(), shortName.getText().toString(), runnerIds);

                //go to team page
                Fragment tf = new TeamFragment();
                Bundle args = new Bundle();
                args.putInt("key", AppData.teams.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