I’m trying to write a simple baseball scorekeeping app, mostly as a learning exercise.
At this point I have a fully functional app. It has a single activity and a single layout. All the numbers (balls, strikes, outs, innings) are displayed as buttons. I have extended the android.widget.Button class so that the button’s text is an integer, and used onClick to increment the value by 1 when the button is clicked. The object also stores a maximum value; when the increment reaches that value, the counter is reset to 0. So for example the “Balls” button carries a max value of 4 and counts 0, 1, 2, and 3. When you hit it the next time it flips from 3 back to 0. (Apologies to those who don’t know or care anything about baseball.)
All this works fine as far as it goes (source code for my extended button class shown below). Now, I’m trying to change it so that when one counter goes back to 0, the others do also. I’m at a loss as to how to do this. My first instinct was to just add in to the same ‘if’ statement that flips the value back to 0 like so:
final ScoreButton strikes = (ScoreButton) findViewById(R.id.strikes);
strikes.zero();
(where “strikes” is a ScoreButton object defined in the activity). This comes back with a Null Pointer error.
My second thought was to add a boolean attribute that the increment method could set when it goes back to 0 (“reset”). But I don’t understand where to read this attribute. I can check it once in the onResume method, but trying to do something like a “while” loop to read the variable repeatedly just locked up the app without even displaying the main layout.
Trying to research a better way to do this led me to reading about AsyncTask, which seems like overkill, and I’m not sure it would even work since the task (namely checking to see if a specific button has been reset) doesn’t end.
At this point it seems like this is something so simple to do that I must have missed something obvious. I’d appreciate any suggestions you have.
Code for my custom button class:
import android.widget.Button;
import android.content.Context;
import android.view.View;
import android.util.AttributeSet;
public class ScoreButton extends Button {
protected int flipCount;
protected int currCount;
protected boolean reset;
public ScoreButton(Context context) {
super(context);
}
public ScoreButton(Context context, AttributeSet attr) {
super(context, attr);
setOnClickListener(incr);
setOnLongClickListener(dec);
}
public void init(int start, int max ) {
flipCount = max; /** number at which the counter goes back to 0 **/
currCount = start; /** number to start at **/
reset = false;
setText(Integer.toString(currCount));
}
/** reset the button value to 0 **/
public void zero() {
currCount = 0;
setText(Integer.toString(currCount));
}
private OnClickListener incr = new OnClickListener() {
public void onClick(View v) {
currCount++; /** increment number on button **/
if (currCount == flipCount) { /** if at the maximum value, go back to 0 **/
currCount = 0;
reset = true;
final ScoreButton strikes = (ScoreButton) findViewById(R.id.strikes);
strikes.zero();
}
setText(Integer.toString(currCount)); /** display the new button text **/
}
} ;
/** this method decreases the value by 1 on a long click **/
private OnLongClickListener dec = new OnLongClickListener() {
public boolean onLongClick(View v) {
currCount--;
if (currCount == -1) {
currCount=0;
}
setText(Integer.toString(currCount));
return true;
}
} ;
}
You are correct to not attempt the
AsynkTaskmethod you described. That is not correct.Here’s my suggestion:
You can give all the
ScoreButtons a more complexonClickListenerthat you would define in your Activity (from your Activity’s scope, not theScoreButtonclass’s scope). This would be ONE listener that contains information about all theButtons as member variables. You would only create one instance of it but pass it to all yourScoreButtons [orButtons throughsetOnClickListener(). When one of the scores needs to be wrapped, you simply set all the other counters in the Listener to 0. [Note: In this approach, you would not even need to subclassButtonalthough you can if you want].However, since you said you’re learning, you might even try creating a
ScoreButtonGroupViewthat you could place in your view hierarchy. This would be a nice self-contained solution that has internal logic for setting the buttons’ values appropriately (as I think you were trying to do by subclassingButton).