Storing variables of Android Views (button, TextViews etc.)

I currently have an application that is storing around 10 variables per android view in which all of the views need to have their variables changed dynamically. How would be the best way of storing these variables in an efficient way and or would be less code because as of right now I have around 200 variables and each one being called like 4 times to change the variables.

So like this:

String buttonName = "blah";

buttonName = newValue;

Would using an ArrayList save more code and help OOD? Also most of the views have ints, floats, & String variables assigned to them. I need a good way to save code that could possibly work for multiple Views that all need different values. Any Ideas? Any help would be greatly appreciated! Thanks!

Upvotes: 3

Views: 2564

Answers (3)

FoamyGuy
FoamyGuy

Reputation: 46856

I am not sure that I understand completely what you mean. But I'll give it a go.

If I were you I'd create myself a "Holder" class that contains all the variables that you need to store contained in it. Then once you instantiate an Object of that class and populate the values you can "attach" it to a particular view using the view.setTag(Object); method. Whenever you need to retrieve them you can find a reference to your holder object with (YourHolderClass)view.getTag(); Note that getTag will return a plain object, you must cast it. The tag object will stick with this view the whole time it is around so you are always able to retrieve it

edit: View.setTag() javadoc

Upvotes: 2

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28389

I would extend whatever Views you are using and store the additional variables in your extended class...

 //constructor
 public class MyButton extends Button{
   public String someVariable;
   public Button(Context c){
       super(c);
   }

 }

then in your code you would use it like...

MyButton b = new MyButton(this);
b.someVariable = "foofy"; 

Upvotes: 2

michaelg
michaelg

Reputation: 2692

Do you need to change these variables and then have their new values displayed in the UI? If so you can simply have a method for setting the display values in the UI. You could call this during onCreate or onResume, and then again anytime some values have changed. You could also use encapsulate with a custom View and then call invalidate so that it gets redrawn.

Upvotes: 0

Related Questions