Skip to content
Snippets Groups Projects
Commit 9bd32af5 authored by yusenw2's avatar yusenw2
Browse files

part3

parent 6481e238
No related branches found
No related tags found
No related merge requests found
Pipeline #180272 failed
File added
package com.cs498MD.SportsRecorder;
enum Type {Attempt, Score, Foul;}
public class Action {
private String teamName;
Type type;
Integer point;
String message;
public Type getType() {
return type;
}
public Action(String teamName, Type type, Integer point) {
this.teamName = teamName;
this.type = type;
this.point = point;
if (type == Type.Attempt) {
this.message = teamName + " missed!";
} else if (type == Type.Score) {
this.message = teamName + " got " + point + " point" + (point == 1 ? "!" : "s!");
} else {
this.message = teamName + " made a foul!";
}
}
public void setType(Type type) {
this.type = type;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.cs498MD.SportsRecorder;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.ArrayList;
import static android.content.Context.MODE_PRIVATE;
public class ActionListAdapter extends BaseAdapter {
private ArrayList<String> list;
private ArrayList<String> matchIdList;
private Context context;
private Activity activity;
private String MATCH = "match";
public ActionListAdapter(ArrayList<String> list, ArrayList<String> matchIdList, Context context, Activity activity) {
this.list = list;
this.context = context;
this.activity = activity;
this.matchIdList = matchIdList;
}
public ActionListAdapter(ArrayList<String> list, Context context, Activity activity) {
this.list = list;
this.context = context;
this.activity = activity;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
public String getMatchId(int pos) {
return matchIdList.get(pos);
}
public void clear() {
list.clear();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.action_list_layout, null);
}
//Handle TextView and display string from your list
final TextView listItemText = (TextView)view.findViewById(R.id.action_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button delete_btn = (Button)view.findViewById(R.id.action_delete_btn);
delete_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //delete action
notifyDataSetChanged();
}
});
return view;
}
}
\ No newline at end of file
package com.cs498MD.SportsRecorder;
import android.app.Application;
import android.graphics.Typeface;
/**
* Copied from github
* For original author, go to: https://github.com/Yalantis/GuillotineMenu-Android
*/
public class App extends Application {
private static final String CANARO_EXTRA_BOLD_PATH = "fonts/canaro_extra_bold.otf";
public static Typeface canaroExtraBold;
@Override
public void onCreate() {
super.onCreate();
initTypeface();
}
private void initTypeface() {
canaroExtraBold = Typeface.createFromAsset(getAssets(), CANARO_EXTRA_BOLD_PATH);
}
}
package com.cs498MD.SportsRecorder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.google.gson.Gson;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class GameStats extends AppCompatActivity implements View.OnClickListener {
private static final Integer SCORE_TABLE = 0;
private static final Integer TEAM_TABLE = 1;
private static final Integer PLAYER_TABLE = 2;
private Integer[] teamPeriodScores = {0, 0, 0, 0, 0, 0}; // 0th Period is the Total Game Score
private Integer[] oppPeriodScores = {0, 0, 0, 0, 0, 0};
private Integer[] gameBreakDown = {0, 0, 0, 0, 0};
private Integer[] myKidBreakDown = {0, 0, 0, 0, 0};
private Integer[] othersBreakDown = {0, 0, 0, 0, 0};
private String myKidName;
private static TextView scores;
private String matchJson;
private Match match;
private String matchName;
private TableLayout scoreTable;
private TableLayout teamTable;
private TableLayout playerTable;
private static String[] SCORE_HEADER = {"Team", "Total", "QTR 1", "QTR 2", "QTR 3", "QTR 4", "QTR 4+"};
private static String[] TEAM_HEADER = {"Total", "1 PT", "2 PT", "3 PT", "Foul"};
private static String[] PLAYER_HEADER = {"Player", "Total", "1 PT", "2 PT", "3 PT", "Foul"};
private void formatHeaderText(TextView tv, String text) {
tv.setText(text);
tv.setPadding(32, 0, 32, 0);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv.setTextSize(20);
tv.setTypeface(null, Typeface.BOLD);
tv.setBackgroundResource(R.drawable.cell_shape);
}
private void formatTableText(TextView tv) {
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv.setPadding(32, 0, 32, 0);
tv.setTextSize(20);
}
private void createHeaderRow(Integer table) {
switch (table) {
case 0:
TableRow scoreHeader = new TableRow(GameStats.this);
// Set header for score breakdown table
for (int i = 0; i < SCORE_HEADER.length; i++) {
TextView tv = new TextView(GameStats.this);
formatHeaderText(tv, SCORE_HEADER[i]);
if (i < SCORE_HEADER.length) { tv.setBackgroundResource(R.drawable.cell_shape); }
scoreHeader.addView(tv);
}
scoreTable.addView(scoreHeader);
break;
case 1:
TableRow teamHeader = new TableRow(GameStats.this);
for (int i = 0; i < TEAM_HEADER.length; i++) {
TextView tv = new TextView(GameStats.this);
formatHeaderText(tv, TEAM_HEADER[i]);
if (i < TEAM_HEADER.length) { tv.setBackgroundResource(R.drawable.cell_shape); }
teamHeader.addView(tv);
}
teamTable.addView(teamHeader);
break;
case 2:
TableRow playerHeader = new TableRow(GameStats.this);
for (int i = 0; i < PLAYER_HEADER.length; i++) {
TextView tv = new TextView(GameStats.this);
formatHeaderText(tv, PLAYER_HEADER[i]);
if (i < PLAYER_HEADER.length) { tv.setBackgroundResource(R.drawable.cell_shape); }
playerHeader.addView(tv);
}
playerTable.addView(playerHeader);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.stats_menu, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_stats);
// findViewById(R.id.send).setOnClickListener(this);
scores = findViewById(R.id.score);
scores.setBackgroundResource(R.drawable.border_bottom);
scoreTable = findViewById(R.id.overallStats);
teamTable = findViewById(R.id.teamStats);
playerTable = findViewById(R.id.playerStats);
String matchId = getIntent().getStringExtra("matchId");
SharedPreferences sharedPreferences = getSharedPreferences("match", MODE_PRIVATE);
matchJson = sharedPreferences.getString(matchId, "");
Gson gson = new Gson();
match = gson.fromJson(matchJson, Match.class);
matchName = match.getName();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setTitle(matchName);
int periodCount = 1;
Period[] periods = match.getPeriods();
for (int i = 0; i < periods.length; i++){
Period period = periods[i];
Team oppTeam = period.getOpponent();
oppPeriodScores[0] += oppTeam.getScore();
oppPeriodScores[periodCount] += (oppTeam.getScore());
Team myKid = period.getKid();
Team others = period.getOthers();
teamPeriodScores[0] += myKid.getScore() + others.getScore();
teamPeriodScores[periodCount++] += myKid.getScore() + others.getScore();
// PLAYER STUFF!!
myKidBreakDown[0] += myKid.getScore();
myKidBreakDown[1] += myKid.getOnePoint();
myKidBreakDown[2] += myKid.getTwoPoint();
myKidBreakDown[3] += myKid.getThreePoint();
myKidBreakDown[4] += myKid.getMiss();
othersBreakDown[0] += others.getScore();
othersBreakDown[1] += others.getOnePoint();
othersBreakDown[2] += others.getTwoPoint();
othersBreakDown[3] += others.getThreePoint();
othersBreakDown[4] += others.getMiss();
myKidName = myKid.getName();
if (periodCount > 5) {
periodCount = 5;
}
gameBreakDown[0] += myKid.getScore() + others.getScore();
gameBreakDown[1] += myKid.getOnePoint() + others.getOnePoint();
gameBreakDown[2] += myKid.getTwoPoint() + others.getTwoPoint();
gameBreakDown[3] += myKid.getThreePoint() + others.getThreePoint();
gameBreakDown[4] += myKid.getMiss() + others.getMiss();
}
Log.d("TEAM DEBUG", match.getName());
populateScoreTable();
// populateTeamTable();
populatePlayerTable();
// Hiding Text for Team Table
// ((TextView) findViewById(R.id.teamStatsTitle)).setText("");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.stats_menu_id) {
Log.e("TEST", "clicked");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// if (v.getId() == R.id.send) {
// Intent sendIntent = new Intent();
// sendIntent.setAction(Intent.ACTION_SEND);
// sendIntent.putExtra(Intent.EXTRA_TEXT, matchJson);
// sendIntent.setType("text/plain");
//
// if (sendIntent.resolveActivity(getPackageManager()) != null) {
// startActivity(sendIntent);
// }
// }
}
private void populateScoreTable() {
// Create Header Row
createHeaderRow(SCORE_TABLE);
// TODO: Extract Team names from Shared Preferences
String[] teams = {"My Team", "Opponent"};
for (int i = 0; i < teams.length; i++) {
TableRow row = new TableRow(GameStats.this);
TextView tv = new TextView(GameStats.this);
formatTableText(tv);
tv.setTextColor(i==0 ? 0xFFC0392B:0xFF2980B9);
tv.setText(teams[i]);
row.addView(tv);
for (int j = 0; j < teamPeriodScores.length; j++) {
tv = new TextView(GameStats.this);
formatTableText(tv);
tv.setText(i == 0 ? teamPeriodScores[j].toString() : oppPeriodScores[j].toString());
row.addView(tv);
}
scoreTable.addView(row);
}
}
private void populateTeamTable() {
createHeaderRow(TEAM_TABLE);
TableRow row = new TableRow(GameStats.this);
for (int i = 0; i < TEAM_HEADER.length; i++) {
TextView tv = new TextView(GameStats.this);
formatTableText(tv);
tv.setText(gameBreakDown[i].toString());
row.addView(tv);
}
teamTable.addView(row);
}
private void populatePlayerTable() {
// Only create Players Table if there are players to keep track of
String[] players = {myKidName, "Others", "Overall"};
createHeaderRow(PLAYER_TABLE);
TableRow kidRow = new TableRow(GameStats.this);
TableRow otherRow = new TableRow(GameStats.this);
TableRow overallRow = new TableRow(GameStats.this);
for (int i = 0; i < players.length; i++) {
TextView tv = new TextView(GameStats.this);
formatTableText(tv);
switch (i) {
case 0:
tv.setText(players[i]);
kidRow.addView(tv);
break;
case 1:
tv.setText(players[i]);
otherRow.addView(tv);
break;
case 2:
tv.setText(players[i]);
overallRow.addView(tv);
break;
}
}
for (int i = 0; i < PLAYER_HEADER.length - 1; i++) {
TextView kid = new TextView(GameStats.this);
TextView other = new TextView(GameStats.this);
TextView overall = new TextView(GameStats.this);
formatTableText(kid);
formatTableText(other);
formatTableText(overall);
int kidScore = myKidBreakDown[i];
int otherScore = othersBreakDown[i];
kid.setText(Integer.toString(kidScore));
other.setText(Integer.toString(otherScore));
overall.setText(Integer.toString(kidScore + otherScore));
kidRow.addView(kid);
otherRow.addView(other);
overallRow.addView(overall);
}
playerTable.addView(kidRow);
playerTable.addView(otherRow);
playerTable.addView(overallRow);
// for (String player : players) {
// TableRow row = new TableRow(GameStats.this);
// TextView tv = new TextView(GameStats.this);
// formatTableText(tv);
// tv.setText(player);
// row.addView(tv);
//
// for (int i = 0; i < PLAYER_HEADER.length - 1; i++) {
// tv = new TextView(GameStats.this);
// formatTableText(tv);
// tv.setText(player.equals("Others") ? othersBreakDown[i].toString() : myKidBreakDown[i].toString());
// row.addView(tv);
// }
//
// playerTable.addView(row);
// }
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (match.isDone()) {
startActivity(new Intent(this, MainActivity.class));
}
}
}
\ No newline at end of file
This diff is collapsed.
package com.cs498MD.SportsRecorder;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ArrayList<String> matchNameArray = new ArrayList<>();
private ArrayList<String> matchIdArray = new ArrayList<>();
private MyCustomAdapter adapter;
//bottom sheet
private BottomSheetBehavior bottomSheetBehavior;
private LinearLayout linearLayoutBSheet;
private ToggleButton tbUpDown;
private Button createBtn;
private EditText userInputMatchName;
private EditText userInputKidName;
private String MATCH = "match";
private boolean isNumeric(String num) {
return num.matches("[0-9]+");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_list);
init_bottomsheet();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
tbUpDown.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}else{
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View view, int newState) {
if(newState == BottomSheetBehavior.STATE_EXPANDED){
tbUpDown.setChecked(true);
}else if(newState == BottomSheetBehavior.STATE_COLLAPSED){
tbUpDown.setChecked(false);
}
}
@Override
public void onSlide(View view, float v) {
}
});
ActionBar bar = getSupportActionBar();
bar.setTitle("Matches");
SharedPreferences sharedPreferences = getSharedPreferences(MATCH, MODE_PRIVATE);
Gson gson = new Gson();
Map<String, ?> allEntries = sharedPreferences.getAll();
SortedSet<Integer> keys = new TreeSet<>();
for (String key : allEntries.keySet()) {
if (!isNumeric(key)) {
continue;
}
keys.add(Integer.valueOf(key));
}
Log.d("KEYS", keys.toString());
for (Integer matchId : keys) {
String matchJson = sharedPreferences.getString(matchId.toString(), "");
Match match = gson.fromJson(matchJson, Match.class);
matchNameArray.add(match.getName());
matchIdArray.add(matchId.toString()); // do something
}
adapter = new MyCustomAdapter(matchNameArray, matchIdArray, this, MainActivity.this);
ListView listView = (ListView) findViewById(R.id.matchList);
listView.setEmptyView(findViewById(R.id.noMatches));
listView.setAdapter(adapter);
createBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("match", MODE_PRIVATE);
int matchCount = sharedPreferences.getInt("matchCount", -1);
matchCount++;
String matchName = "Match " + matchCount;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("matchCount", matchCount);
editor.putString("matchId", matchName);
editor.apply();
if(v.getId() == R.id.btnCreate){
Log.e("TEST_SHEET", "Create Match Clicked");
Log.e("TEST_SHEET", "Match: " +userInputMatchName.getText().toString());
Log.e("TEST_SHEET", "Kid: " + userInputKidName.getText().toString());
if(userInputMatchName.getText().toString().equals("")){
//user did not enter match name
Toast.makeText(this, "Please Enter a Match Name", Toast.LENGTH_SHORT).show();
}
if(userInputKidName.getText().toString().equals("")){
//user did not enter kid name
}
//TODO: Change match info details. But right now can be used for testing purpose
matchNameArray.add(userInputMatchName.getText().toString());
matchIdArray.add("" + matchCount);
adapter.notifyDataSetChanged();
Intent intent = new Intent(this, InputActivity.class);
intent.putExtra("matchId", Integer.toString(matchCount));
intent.putExtra("matchName", userInputMatchName.getText().toString());
intent.putExtra("kidName", userInputKidName.getText().toString());
startActivity(intent);
}
}
//bottom sheet
private void init_bottomsheet() {
this.linearLayoutBSheet = findViewById(R.id.bottomSheet);
this.bottomSheetBehavior = BottomSheetBehavior.from(linearLayoutBSheet);
this.tbUpDown = findViewById(R.id.toggleButton);
this.createBtn = findViewById(R.id.btnCreate);
this.userInputKidName = findViewById(R.id.kidNameText);
this.userInputMatchName = findViewById(R.id.matchNameText);
}
private SimpleAdapter getAdapterListViewCT(ArrayList<Map<String, Object>> lista) {
return new SimpleAdapter(this, lista,
android.R.layout.simple_list_item_2, new String[]{"Cantante", "Titulo"},
new int[]{android.R.id.text1, android.R.id.text2}) {
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView txtNombre = view.findViewById(android.R.id.text1);
txtNombre.setTypeface(Typeface.DEFAULT_BOLD);
TextView txtCorreo = view.findViewById(android.R.id.text2);
txtCorreo.setTextColor(Color.DKGRAY);
return view;
}
};
}
private int getRandom() {
return (int) Math.floor(Math.random() * 100);
}
}
\ No newline at end of file
package com.cs498MD.SportsRecorder;
import java.util.Stack;
public class Match {
private int id;
private String name;
private Period[] periods = new Period[5];
private boolean isDone;
public Match(int id) {
this.id = id;
this.name = "Match " + id;
for (int i = 0; i < periods.length; i++) {
periods[i] = new Period();
}
}
public boolean isDone() {
return isDone;
}
public void setDone(boolean done) {
isDone = done;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Period[] getPeriods() {
return periods;
}
public void setPeriods(Period[] periods) {
this.periods = periods;
}
}
package com.cs498MD.SportsRecorder;
import android.app.Activity;
import android.os.Bundle;
public class MatchList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_list);
}
}
package com.cs498MD.SportsRecorder;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.ArrayList;
import static android.content.Context.MODE_PRIVATE;
public class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> list;
private ArrayList<String> matchIdList;
private Context context;
private Activity activity;
private String MATCH = "match";
public MyCustomAdapter(ArrayList<String> list, ArrayList<String> matchIdList, Context context, Activity activity) {
this.list = list;
this.context = context;
this.activity = activity;
this.matchIdList = matchIdList;
}
public MyCustomAdapter(ArrayList<String> list, Context context, Activity activity) {
this.list = list;
this.context = context;
this.activity = activity;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
public String getMatchId(int pos) {
return matchIdList.get(pos);
}
public void clear() {
list.clear();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.match_list_layout, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.match_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button delete_btn = (Button)view.findViewById(R.id.delete_btn);
Button viewBtn = (Button)view.findViewById(R.id.view_btn);
delete_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
SharedPreferences sharedPreferences = context.getSharedPreferences(MATCH, MODE_PRIVATE);
sharedPreferences.edit().remove(getMatchId(position)).apply();
list.remove(position); //or some other task
matchIdList.remove(position);
notifyDataSetChanged();
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to delete this match?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
}
});
viewBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), GameStats.class);
intent.putExtra("matchId", matchIdList.get(position));
v.getContext().startActivity(intent);
}
});
return view;
}
}
\ No newline at end of file
package com.cs498MD.SportsRecorder;
import java.util.Stack;
public class Period {
private Team others;
private Team kid;
private Team opponent;
private Stack<Action> history;
public Period() {
this.others = new Team("My Team (Others)");
this.kid = new Team("My Kid");
this.opponent = new Team("Opponent Team");
this.history = new Stack<>();
}
public Team getOthers() {
return others;
}
public void setOthers(Team others) {
this.others = others;
}
public Team getKid() {
return kid;
}
public void setKid(Team kid) {
this.kid = kid;
}
public Team getOpponent() {
return opponent;
}
public void setOpponent(Team opponent) {
this.opponent = opponent;
}
public Stack<Action> getHistory() {
return history;
}
public void setHistory(Stack<Action> history) {
this.history = history;
}
}
package com.cs498MD.SportsRecorder;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.RecyclerView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private ArrayList<String> data;
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mTitle;
RelativeLayout relativeLayout;
public MyViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.txtTitle);
}
// @Override
// public void onClick(View v) {
// if (v.getId() == R.id.action_delete_btn){
// Log.e("TEST", "Action deleted");
// }
//
// }
}
public RecyclerViewAdapter(ArrayList<String> data) {
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.mTitle.setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public void removeItem(int position) {
data.remove(position);
notifyItemRemoved(position);
}
public void restoreItem(String item, int position) {
data.add(position, item);
notifyItemInserted(position);
}
public ArrayList<String> getData() {
return data;
}
}
package com.cs498MD.SportsRecorder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
abstract public class SwipeToDeleteCallback extends ItemTouchHelper.Callback {
Context mContext;
private Paint mClearPaint;
private ColorDrawable mBackground;
private int backgroundColor;
private Drawable deleteDrawable;
private int intrinsicWidth;
private int intrinsicHeight;
SwipeToDeleteCallback(Context context) {
mContext = context;
mBackground = new ColorDrawable();
backgroundColor = Color.parseColor("#b80f0a");
mClearPaint = new Paint();
mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
deleteDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_icons8_trash_can);
intrinsicWidth = deleteDrawable.getIntrinsicWidth();
intrinsicHeight = deleteDrawable.getIntrinsicHeight();
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, ItemTouchHelper.LEFT);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView;
int itemHeight = itemView.getHeight();
boolean isCancelled = dX == 0 && !isCurrentlyActive;
if (isCancelled) {
clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
return;
}
mBackground.setColor(backgroundColor);
mBackground.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
mBackground.draw(c);
int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
int deleteIconRight = itemView.getRight() - deleteIconMargin;
int deleteIconBottom = deleteIconTop + intrinsicHeight;
deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom);
deleteDrawable.draw(c);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
private void clearCanvas(Canvas c, Float left, Float top, Float right, Float bottom) {
c.drawRect(left, top, right, bottom, mClearPaint);
}
@Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return 0.7f;
}
}
package com.cs498MD.SportsRecorder;
import java.util.ArrayList;
public class Team {
private String name;
private int score;
private int onePoint;
private int twoPoint;
private int threePoint;
private int miss;
public Team(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOnePoint() {
return onePoint;
}
public void setOnePoint(int onePoint) {
this.onePoint = onePoint;
}
public int getTwoPoint() {
return twoPoint;
}
public void setTwoPoint(int twoPoint) {
this.twoPoint = twoPoint;
}
public int getThreePoint() {
return threePoint;
}
public void setThreePoint(int threePoint) {
this.threePoint = threePoint;
}
public int getMiss() {
return miss;
}
public void setMiss(int miss) {
this.miss = miss;
}
}
package com.cs498MD.SportsRecorder;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
public class test_activity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerViewAdapter mAdapter;
ArrayList<String> stringArrayList = new ArrayList<>();
CoordinatorLayout coordinatorLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_recycle);
recyclerView = findViewById(R.id.recyclerView);
coordinatorLayout = findViewById(R.id.coordinatorLayout);
populateRecyclerView();
enableSwipeToDeleteAndUndo();
}
private void populateRecyclerView() {
stringArrayList.add("Item 1");
stringArrayList.add("Item 2");
stringArrayList.add("Item 3");
stringArrayList.add("Item 4");
stringArrayList.add("Item 5");
stringArrayList.add("Item 6");
stringArrayList.add("Item 7");
stringArrayList.add("Item 8");
stringArrayList.add("Item 9");
stringArrayList.add("Item 10");
mAdapter = new RecyclerViewAdapter(stringArrayList);
recyclerView.setAdapter(mAdapter);
}
private void enableSwipeToDeleteAndUndo() {
SwipeToDeleteCallback swipeToDeleteCallback = new SwipeToDeleteCallback(this) {
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
final int position = viewHolder.getAdapterPosition();
final String item = mAdapter.getData().get(position);
mAdapter.removeItem(position);
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Item was removed from the list.", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
mAdapter.restoreItem(item, position);
recyclerView.scrollToPosition(position);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
};
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeToDeleteCallback);
itemTouchhelper.attachToRecyclerView(recyclerView);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment