-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestCourseDisplayPage.java
194 lines (169 loc) · 8.47 KB
/
BestCourseDisplayPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.scoresheet.discgolf;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
/**
* Created by Joe Post on 5/28/2017.
*/
public class BestCourseDisplayPage extends Fragment implements View.OnClickListener{
public BestCourseDisplayPage(){
}
View myView;
ArrayList<Integer> net_scores = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
DatabaseHelperScoringTable scoringtableDBH = new DatabaseHelperScoringTable(getActivity());
myView = inflater.inflate(R.layout.unique_course_info, container, false);
//find all buttons and set onClickListeners for each
final Button back_button = (Button) myView.findViewById(R.id.back_button);
back_button.setOnClickListener(this);
Bundle args = this.getArguments();
TextView title = (TextView) myView.findViewById(R.id.unique_course_name);
DatabaseHelper DBH = new DatabaseHelper(getActivity());
title.setText("Course: " + DBH.getCourseName(args.getInt("course_id")));
TableLayout score_table = (TableLayout) myView.findViewById(R.id.score_table);
TableLayout name_table = (TableLayout) myView.findViewById(R.id.name_table);
//initialize hole and par textviews
TextView hole_tv = new TextView(getActivity());
hole_tv.setText("Hole");
hole_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
TableRow tr_hole = new TableRow(getActivity());
tr_hole.addView(hole_tv);
name_table.addView(tr_hole, new TableLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
TextView par_tv = new TextView(getActivity());
par_tv.setText("Par");
par_tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
TableRow tr_par = new TableRow(getActivity());
tr_par.addView(par_tv);
name_table.addView(tr_par, new TableLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
//now get specific course information
int unique_course_id = args.getInt("unique_course_id"); //this sole piece of info is required
String player_to_display_best_course = args.getString("name");
List<String> player_names = scoringtableDBH.getPlayerNamesforUniqueCourse(unique_course_id);
List<Integer> holes = scoringtableDBH.getHolesForUniqueCourse(unique_course_id, player_names.get(0));
ArrayList<Integer> pars = scoringtableDBH.getParsForUniqueCourse(unique_course_id, player_names.get(0));
// for(int i = 0; i < player_names.size(); i++){
// TableRow row_for_player_name = new TableRow(getActivity());
// TextView tv = new TextView(getActivity());
// tv.setText(player_names.get(i));
// tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
// row_for_player_name.addView(tv);
// name_table.addView(row_for_player_name, new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
// }
TableRow row_for_hole_nums = new TableRow(getActivity());
row_for_hole_nums.setOrientation(TableRow.HORIZONTAL);
for(int i = 0; i < holes.size(); i++){
TextView tv = new TextView(getActivity());
tv.setText(holes.get(i).toString());
tv.setWidth(100);
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
row_for_hole_nums.addView(tv);
}
score_table.addView(row_for_hole_nums, new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
TableRow row_for_par_nums = new TableRow(getActivity());
row_for_par_nums.setOrientation(TableRow.HORIZONTAL);
for(int i = 0; i < pars.size(); i++){
TextView tv = new TextView(getActivity());
tv.setText(pars.get(i).toString());
tv.setWidth(50);
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
row_for_par_nums.addView(tv);
}
score_table.addView(row_for_par_nums, new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
int best_player_score = 0;
for(int i = 0; i < player_names.size(); i++){
ArrayList<Integer> player_scores
= scoringtableDBH.getPlayerScoresUniqueCourse(unique_course_id, player_names.get(i));
TableRow row_for_player_score = new TableRow(getActivity());
row_for_player_score.setOrientation(TableRow.HORIZONTAL);
net_scores.add(i, getPlayerScore(player_scores, pars));
TextView tv = new TextView(getActivity());
if(score_table.getChildCount() > 0) {
if (net_scores.get(i) >= 0) {
tv.setText(player_names.get(i) + " (+" + net_scores.get(i) + ")");
} else {
tv.setText(player_names.get(i) + " (" + net_scores.get(i) + ")");
}
} else {
tv.setText(player_names.get(i));
}
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
TableRow tr = new TableRow(getActivity());
tr.addView(tv);
//now add to name_table
name_table.addView(tr, new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
if(player_names.get(i).equalsIgnoreCase(player_to_display_best_course)){
best_player_score = getPlayerScore(player_scores, pars);
}
for(int j = 0; j < player_scores.size(); j++){
TextView tv1 = new TextView(getActivity());
tv1.setText(player_scores.get(j).toString());
tv1.setWidth(50);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.f);
row_for_player_score.addView(tv1);
}
score_table.addView(row_for_player_score, new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
}
//
// Integer best_score = net_scores.get(0);
// String best_name = player_names.get(0);
// for (int j = 1; j < net_scores.size(); j++) {
// if (best_score < net_scores.get(j)) {
// best_score = best_score;
// } else {
// best_score = net_scores.get(j);
// best_name = player_names.get(j);
// }
// }
String best = null;
// if(best_score >= 0) {
// best = "Leader: " + best_name + " at " + "+" + Integer.toString(best_score);
// } else if (best_score < 0){
// best = "Leader: " + best_name + " at " + Integer.toString(best_score);
// }
if(best_player_score >= 0) {
best = player_to_display_best_course + "'s Best Score: +" + Integer.toString(best_player_score);
} else if (best_player_score < 0){
best = player_to_display_best_course + "'s Best Score: " + Integer.toString(best_player_score);
}
TextView tv = (TextView) myView.findViewById(R.id.leader);
tv.setText(best);
return myView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_button:
FragmentManager fragmentManager2 = getFragmentManager();
AllTimeBestScoreNames atbsn = new AllTimeBestScoreNames();
Bundle args = new Bundle();
args.putInt("course_id", this.getArguments().getInt("course_id"));
atbsn.setArguments(args);
fragmentManager2.beginTransaction()
.replace(R.id.content_frame, atbsn)
.addToBackStack(null)
.commit();
break;
}
}
public Integer getPlayerScore(ArrayList<Integer> scores, ArrayList<Integer> pars){
Integer netscore = 0;
for(int i = 0; i < scores.size(); i++){
netscore = netscore + (scores.get(i) - pars.get(i));
}
return netscore;
}
}