1

As part of teaching myself to code Android Apps, I am writing an App to keep track of some numbers for a Board Game. As my screenshot shows, I have several values, with minus/plus buttons. The Ship (implemented as a Ship Class) has 6 Values, and therefore 12 Buttons, and 6 TextViews.

As the Game progresses, the Numbers will change, and the User should be able to click the -/+ buttons to reflect this.

Screenshot

Initially, I wrote a separate onClickListener for each Button, updating the Ship Value, and setting the TextView to the new Ship Value.

I then decided to consolidate this into a single OnClickListener, attached to each Button, and a Switch/Case statement, with a case for each Button, that updated the clicked Ship Value, and then re-displayed the Values.

In another attempt to consolidate my code, I wrote a Class to implement a Number Picker (based upon this code: https://stackoverflow.com/a/56061379/3902474 )

The problem that I now have is that although the Number Picker can update the TextViews, I do not know how to get it to update the Ship Values.

The Number Picker does not have access to the Ship Class (and I would like to re-use it for several other types of values).

Is there a way of setting the Ship Values, using this Number Picker, while keeping the Number Picker generic enough to use for other Values/Classes?

Black Spike
  • 142
  • 1
  • 9
  • Would SharedPreferences not do the trick for you? you can assign values to SP in your number picker and read from them when you setup your Ship class, using more switch/case statements would allow you to do the same for the other uses you have in mind – Stitches S Nov 14 '19 at 00:56
  • The Ship Class is set up right at the start of MainActivity. I need to be making numerous changes to it. Number Picker knows when I clicked -/+, but how do I pass that information to MainActivity/ShipClass? How would Main/Ship know to update itself from SharedPreferences? – Black Spike Nov 14 '19 at 01:00

0 Answers0