/
ScheduleAdapter.java
100 lines (84 loc) · 2.92 KB
/
ScheduleAdapter.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
package com.lsiegert;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class ScheduleAdapter extends SimpleCursorAdapter implements OnClickListener{
private Context context;
private Cursor games;
private DatabaseHelper dbHelper;
public ScheduleAdapter(Context context,
int layout,
Cursor c,
String[] from,
int[] to,
DatabaseHelper dbHelper) {
super(context, 0, c, new String[0], new int[0]);
this.context = context;
this.games = c;
this.dbHelper = dbHelper;
}
public Object getItem(int position) {
return games.moveToPosition(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
games.moveToPosition(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.schedule_row, null);
}
TextView gameDate = (TextView) convertView.findViewById(R.id.date);
gameDate.setText(games.getString(1));
TextView gameOpponent = (TextView) convertView.findViewById(R.id.opponent);
String location = games.getString(7);
String opponent = games.getString(3);
if (location.contains("home")) {
gameOpponent.setText("vs " + opponent);
}
else if (location.contains("away")) {
gameOpponent.setText("@ " + opponent);
}
else {
gameOpponent.setText(opponent);
}
TextView gameScore = (TextView) convertView.findViewById(R.id.score);
int nuscore = games.getInt(4);
int oppscore = games.getInt(5);
gameScore.setText(nuscore + "-" + oppscore);
if (nuscore > oppscore) {
gameScore.setTextColor(Color.GREEN);
}
else if (nuscore < oppscore) {
gameScore.setTextColor(Color.RED);
}
else { gameScore.setTextColor(Color.WHITE); }
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.attended);
checkBox.setTag(Integer.parseInt(this.games.getString(this.games.getColumnIndex("_id"))));
if (this.games.getString(this.games.getColumnIndex("attended")) != null
&& Integer.parseInt(this.games.getString(this.games.getColumnIndex("attended"))) != 0) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setOnClickListener(this);
return convertView;
}
public void onClick(View view) {
CheckBox checkbox = (CheckBox) view;
Integer _id = (Integer) checkbox.getTag();
ContentValues values = new ContentValues();
values.put(" attended", checkbox.isChecked() ? 1 : 0);
this.dbHelper.myDb.update("Games", values, "_id=?", new String[]{Integer.toString(_id)});
}
}