From 7f978e4a3fca594150ededec7e13776e0c5e09de Mon Sep 17 00:00:00 2001 From: jvvlives2005 Date: Sun, 30 Aug 2015 18:24:44 -0400 Subject: [PATCH] Final Assessment--semi completed --- build.gradle | 2 +- src/main/AndroidManifest.xml | 3 - src/main/java/nyc/c4q/CustomListAdapter.java | 64 ++++++ src/main/java/nyc/c4q/LibraryActivity.java | 4 +- src/main/java/nyc/c4q/ListActivity.java | 40 +++- .../java/nyc/c4q/PaceCalculatorActivity.java | 22 +- .../java/nyc/c4q/PaceCalculatorFragment.java | 198 ++++++++++++++++++ src/main/res/layout/activity_list.xml | 5 +- .../res/layout/activity_pace_calculator.xml | 14 +- src/main/res/values/strings.xml | 3 + 10 files changed, 340 insertions(+), 15 deletions(-) create mode 100644 src/main/java/nyc/c4q/CustomListAdapter.java create mode 100644 src/main/java/nyc/c4q/PaceCalculatorFragment.java diff --git a/build.gradle b/build.gradle index 7c6b66b..dbace74 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ android { buildToolsVersion "22.0.1" defaultConfig { - minSdkVersion 18 + minSdkVersion 17 targetSdkVersion 22 versionCode 2 versionName "1.0.0-SNAPSHOT" diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index f96e9d3..a590e83 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -16,9 +16,6 @@ - diff --git a/src/main/java/nyc/c4q/CustomListAdapter.java b/src/main/java/nyc/c4q/CustomListAdapter.java new file mode 100644 index 0000000..c4e9be2 --- /dev/null +++ b/src/main/java/nyc/c4q/CustomListAdapter.java @@ -0,0 +1,64 @@ +package nyc.c4q; + +/** + */ +import android.app.Activity; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import java.util.List; + +public class CustomListAdapter extends BaseAdapter { + private Activity activity; + private LayoutInflater inflater; + private List people; + + + public CustomListAdapter(Activity activity, List people) { + this.activity = activity; + this.people = people; + } + + @Override + public int getCount() { + return people.size(); + } + + @Override + public Object getItem(int location) { + return people.get(location); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + if (inflater == null) + inflater = (LayoutInflater) activity + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + if (convertView == null) + convertView = inflater.inflate(R.layout.listitem_member, null); + + TextView names = (TextView) convertView.findViewById(R.id.text_name); + TextView houses = (TextView) convertView.findViewById(R.id.text_house); + + // getting movie data for the row + Person m = people.get(position); + + // Names + names.setText("Name"); + + // Houses + houses.setText("House"); + + return convertView; + } +} \ No newline at end of file diff --git a/src/main/java/nyc/c4q/LibraryActivity.java b/src/main/java/nyc/c4q/LibraryActivity.java index ca2a050..2be797a 100644 --- a/src/main/java/nyc/c4q/LibraryActivity.java +++ b/src/main/java/nyc/c4q/LibraryActivity.java @@ -5,7 +5,6 @@ import android.view.View; import android.widget.EditText; - public class LibraryActivity extends Activity { public EditText inputParameter; @@ -53,4 +52,7 @@ public void button_getCheckedOut_onClick(View view) { // earliest due first. } + + //I am not sure where on earth I'm suppose to put the Database. + } diff --git a/src/main/java/nyc/c4q/ListActivity.java b/src/main/java/nyc/c4q/ListActivity.java index 08894ac..62221b2 100644 --- a/src/main/java/nyc/c4q/ListActivity.java +++ b/src/main/java/nyc/c4q/ListActivity.java @@ -1,15 +1,24 @@ package nyc.c4q; -import android.app.Activity; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; +import android.view.View; +import android.widget.Button; import android.widget.ListView; +import android.widget.Toast; +import android.widget.ToggleButton; +import java.util.ArrayList; +import java.util.List; -public class ListActivity extends Activity { - public ListView list; +public class ListActivity extends android.app.ListActivity { + + private ListActivity activity; + private ListView listView; + private CustomListAdapter adapter; + private List resultList = new ArrayList(); + private ToggleButton buttonName; + private Button buttonColor; public static final Person[] PEOPLE = { new Person("Hannah", "Abbott", House.Hufflepuff), @@ -42,12 +51,31 @@ public class ListActivity extends Activity { new Person("Ron", "Weasley", House.Gryffindor) }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); - list = (ListView) findViewById(R.id.list); + listView = (ListView) findViewById(android.R.id.list); + adapter = new CustomListAdapter(this, resultList); + listView.setAdapter(adapter); + + buttonName = (ToggleButton) findViewById(R.id.button_name); + buttonColor = (Button) findViewById(R.id.button_color); + + buttonName.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + StringBuffer result = new StringBuffer(); + //if the click is on + result.append("First and Last").append(); + + //if the click is off + result.append("Last and First ").append(); + Toast.makeText(ListActivity.this, result.toString(), Toast.LENGTH_SHORT).show(); + } + }); } } diff --git a/src/main/java/nyc/c4q/PaceCalculatorActivity.java b/src/main/java/nyc/c4q/PaceCalculatorActivity.java index 5c0616e..ab95077 100644 --- a/src/main/java/nyc/c4q/PaceCalculatorActivity.java +++ b/src/main/java/nyc/c4q/PaceCalculatorActivity.java @@ -1,7 +1,7 @@ package nyc.c4q; -import android.support.v4.app.FragmentActivity; import android.os.Bundle; +import android.support.v4.app.FragmentActivity; public class PaceCalculatorActivity extends FragmentActivity { @@ -9,6 +9,26 @@ public class PaceCalculatorActivity extends FragmentActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pace_calculator); + +// //To have the fragment be shown in the PaceCalculatorActivity +// android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); +// FragmentTransaction transaction = fm.beginTransaction(); +// transaction.replace(R.id.fragment_pace_calculator, new PaceCalculatorFragment()).commit(); + + + +// +// if (savedInstanceState == null) { +// getSupportFragmentManager().beginTransaction() +// .add(R.id.fragment_pace_calculator, new PaceCalculatorFragment()) +// .commit(); +// } + + +// //if you added fragment via layout xml +// PaceCalculatorFragment fragment = (PaceCalculatorFragment)fm.findFragmentById(R.id.fragment_pace_calculator); +// fragment.getView(); + } } diff --git a/src/main/java/nyc/c4q/PaceCalculatorFragment.java b/src/main/java/nyc/c4q/PaceCalculatorFragment.java new file mode 100644 index 0000000..f7cd466 --- /dev/null +++ b/src/main/java/nyc/c4q/PaceCalculatorFragment.java @@ -0,0 +1,198 @@ +package nyc.c4q; + +import android.app.Fragment; +import android.net.Uri; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + + +/** + * A simple {@link Fragment} subclass. + * Activities that contain this fragment must implement the + * {@link PaceCalculatorFragment.OnFragmentInteractionListener} interface + * to handle interaction events. + * Use the {@link PaceCalculatorFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class PaceCalculatorFragment extends android.support.v4.app.Fragment { + //created the variables + private Button btn; + private EditText inputDistance; + private EditText inputTimeMin; + private EditText inputTimeSec; + private EditText inputPaceMin; + private EditText inputPaceSec; + private TextView tvResult; + private int count = 0; //0 because it hasn't been clicked yet + + + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + private OnFragmentInteractionListener mListener; + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment BlankFragment. + */ + // TODO: Rename and change types and number of parameters + public static PaceCalculatorFragment newInstance(String param1, String param2) { + PaceCalculatorFragment fragment = new PaceCalculatorFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + public PaceCalculatorFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View result = inflater.inflate(R.layout.fragment_pace_calculator, container, false); + + inputDistance = (EditText) result.findViewById(R.id.input_distance); + inputTimeMin = (EditText) result.findViewById(R.id.input_time_min); + inputTimeSec = (EditText) result.findViewById(R.id.input_time_sec); + inputPaceMin = (EditText) result.findViewById(R.id.input_pace_min); + inputPaceSec = (EditText) result.findViewById(R.id.input_pace_sec); + btn = (Button) result.findViewById(R.id.button_calculate); + + + //to create a button + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //the idea for this part is to calculate the pace which means pace is equal to minutes/miles (time/distance) + //if the fields where all there, then the calculation should work + if(inputDistance == null) { + //the logic is that the number used in the Edit Text should give the integers and provide this operation + //converting editText to Integers + String stringInputTimeMinTextFromET = inputTimeMin.getText().toString(); + int numberInputTimeMinIntFromET = new Integer(stringInputTimeMinTextFromET).intValue(); + + String stringInputPaceMinTextFromET = inputPaceMin.getText().toString(); + int numberInputPaceMinIntFromET = new Integer(stringInputPaceMinTextFromET).intValue(); + + String stringInputDistanceTextFromET = inputDistance.getText().toString(); + int numberInputDistanceIntFromET = new Integer(stringInputDistanceTextFromET).intValue(); + + numberInputDistanceIntFromET = numberInputTimeMinIntFromET / numberInputPaceMinIntFromET; + + tvResult.setText(String.valueOf(numberInputDistanceIntFromET)); + + } + else if (inputTimeMin == null) { + String stringInputTimeMinTextFromET = inputTimeMin.getText().toString(); + int numberInputTimeMinIntFromET = new Integer(stringInputTimeMinTextFromET).intValue(); + + String stringInputPaceMinTextFromET = inputPaceMin.getText().toString(); + int numberInputPaceMinIntFromET = new Integer(stringInputPaceMinTextFromET).intValue(); + + String stringInputDistanceTextFromET = inputDistance.getText().toString(); + int numberInputDistanceIntFromET = new Integer(stringInputDistanceTextFromET).intValue(); + + numberInputTimeMinIntFromET = numberInputPaceMinIntFromET * numberInputDistanceIntFromET; + + tvResult.setText(String.valueOf(numberInputTimeMinIntFromET)); + + } + + else if (inputPaceMin == null) { + String stringInputTimeMinTextFromET = inputTimeMin.getText().toString(); + int numberInputTimeMinIntFromET = new Integer(stringInputTimeMinTextFromET).intValue(); + + String stringInputPaceMinTextFromET = inputPaceMin.getText().toString(); + int numberInputPaceMinIntFromET = new Integer(stringInputPaceMinTextFromET).intValue(); + + String stringInputDistanceTextFromET = inputDistance.getText().toString(); + int numberInputDistanceIntFromET = new Integer(stringInputDistanceTextFromET).intValue(); + + numberInputPaceMinIntFromET = numberInputTimeMinIntFromET / numberInputDistanceIntFromET; + + tvResult.setText(String.valueOf(numberInputPaceMinIntFromET)); + } + + else{ + //do nothing + return ; + } + + //if one of the fields is missing, then the other parts should be calculated in order to find the missing part + //if time is not there, then pace and distance should be multiplied + //if pace is not there, then time should divide by distance + //if distance is not there, then time should divide by p + count++; //incrementing by 1 + + } + }); + return result; + } + + // TODO: Rename method, update argument and hook method into UI event + public void onButtonPressed(Uri uri) { + if (mListener != null) { + mListener.onFragmentInteraction(uri); + } + } + +// @Override +// public void onAttach(Activity activity) { +// super.onAttach(activity); +// try { +// mListener = (OnFragmentInteractionListener) activity; +// } catch (ClassCastException e) { +// throw new ClassCastException(activity.toString() +// + " must implement OnFragmentInteractionListener"); +// } +// } +// +// @Override +// public void onDetach() { +// super.onDetach(); +// mListener = null; +// } + + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ + public interface OnFragmentInteractionListener { + // TODO: Update argument type and name + public void onFragmentInteraction(Uri uri); + } + +} diff --git a/src/main/res/layout/activity_list.xml b/src/main/res/layout/activity_list.xml index 3e82945..0e9276c 100644 --- a/src/main/res/layout/activity_list.xml +++ b/src/main/res/layout/activity_list.xml @@ -10,13 +10,14 @@ tools:context="nyc.c4q.ListActivity" > -