Brian Gentry
Lurker
I'm trying to learn about Android programming and I've given myself an assignment. I've got a working version that's maybe 70% complete and I'm struggling to go further. I think I have a problem with how I've laid out my program.
The program is a counter. Just an on screen set of buttons and a display that let you count up by one or down by one. Or clear the counter. Pretty simple stuff. However, I want to allow the user to have more than one counter on screen at a time. Any amount that they want actually.
So I created a mainactivity.java and a Counter.java . In the Counter.java I have a Counter class. This class contains all of the data and methods of a counter object. It also builds it's own View and adds the buttons, fields, etc. So the display and the data are encapsulated into the class itself.
Mainactivity instantiates a Counter object on start up. When the user presses the "new counter" button, Mainactivity instantiates another counter object and adds it to the ArrayList that keeps track of all of the counters. With me so far? Good.
After Mainactivity instantiates a Counter, it calls Counter.getLayout(). This returns a LinearLayout object which contains all of the Counter's buttons, etc. MainActivity then stuffs that Layout into the main layout on the main screen. So each new counter appears below the last one.
BTW, Counter class also creates it's own button listener functions, etc. Amazingly this all works. I can build new counters and they all operate independently.
The issues I'm running into are making buttons that will affect each counter, like Delete and Edit. If I build these buttons inside of the counter class, then I need some weird way to get MainActivity to be called when the buttons are pushed, because delete needs to actually remove the counter object, and it was instantiated from Mainactivity. So a button listener inside of Counter won't be able to do the job of removing that counter object and it's Layout.
My workaround right now is that I build a wrapper layout inside of Mainactvity that wraps around the counter's layout. That wrapper has the delete button on it and mainactivity operates on that button, via it's listener, directly. This is ugly and feels WAY wrong.
All of this is starting to tell me that I shouldn't be building the GUI objects inside of the Counter class. I guess I need to build the GUI display for each counter object inside of Mainactivity and then use some sort of data structure to keep track of both the Counter object, and the layout that displays that counter's data, contains it's buttons, button listeners, etc.
I hope this all makes some sense. I'd very much appreciate advice on how to properly structure this.
Thanks for reading.
Brian.
The program is a counter. Just an on screen set of buttons and a display that let you count up by one or down by one. Or clear the counter. Pretty simple stuff. However, I want to allow the user to have more than one counter on screen at a time. Any amount that they want actually.
So I created a mainactivity.java and a Counter.java . In the Counter.java I have a Counter class. This class contains all of the data and methods of a counter object. It also builds it's own View and adds the buttons, fields, etc. So the display and the data are encapsulated into the class itself.
Mainactivity instantiates a Counter object on start up. When the user presses the "new counter" button, Mainactivity instantiates another counter object and adds it to the ArrayList that keeps track of all of the counters. With me so far? Good.

After Mainactivity instantiates a Counter, it calls Counter.getLayout(). This returns a LinearLayout object which contains all of the Counter's buttons, etc. MainActivity then stuffs that Layout into the main layout on the main screen. So each new counter appears below the last one.
BTW, Counter class also creates it's own button listener functions, etc. Amazingly this all works. I can build new counters and they all operate independently.
The issues I'm running into are making buttons that will affect each counter, like Delete and Edit. If I build these buttons inside of the counter class, then I need some weird way to get MainActivity to be called when the buttons are pushed, because delete needs to actually remove the counter object, and it was instantiated from Mainactivity. So a button listener inside of Counter won't be able to do the job of removing that counter object and it's Layout.
My workaround right now is that I build a wrapper layout inside of Mainactvity that wraps around the counter's layout. That wrapper has the delete button on it and mainactivity operates on that button, via it's listener, directly. This is ugly and feels WAY wrong.
All of this is starting to tell me that I shouldn't be building the GUI objects inside of the Counter class. I guess I need to build the GUI display for each counter object inside of Mainactivity and then use some sort of data structure to keep track of both the Counter object, and the layout that displays that counter's data, contains it's buttons, button listeners, etc.
I hope this all makes some sense. I'd very much appreciate advice on how to properly structure this.
Thanks for reading.
Brian.