How to deal with lange amount of views in android?
I'll try to be brief: In my android application I have a large amount of views (30 - 40) to display on a single screen, children to a ScrollView object. Now, said views are nothing but buttons and textviews representing single products details and when pressed, a simple method is called and all I need to pass is a value (int) representing the product. I was wondering how to avoid the hassle of having to type in the code for each view witch would result into dozens of lines of pain:
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1 :
simpleMethod(1);
break;
case R.id.btn2 :
simpleMethod(2);
break;
case R.id.btn3 :
simpleMethod(3);
break;
}
}
..and so forth for all of the views. Consider that there's more than a simpleMethod() to call whenever a button is pressed.
My guess was to use a Map in wit开发者_开发问答ch the key would be the view id aka R.id.something and the value would simply be a number to pass to the method.
What are other efficient ways of handling large amount of views?
you can give tags to your views and then:
public void onClick(View v) {
simpleMethod(Integer.parseInt(v.getTag()));
}
精彩评论