I am programming with android application, but the method I create view is so tedious, anyone have good idea with them?
MapView mMapView1 = new MapView(getActivity(),1);
initialMapView(mMapView1);
MapView mMapView2 = new MapView(getActivity(),2);
initialMapView(mMapView2);
MapView mMapView3 = new MapView(getActivity(),3);
initialMapView(mMapView3);
MapView mMapView4 = new MapView(getActivity(),4);
initialMapView(mMapView4);
MapView mMapView5 = new MapView(getActivity(),5);
initialMapView(mMapView5);
MapView mMapView6 = new MapView(getActivity(),6);
initialMapView(mMapView6);
That looks very much like you could do with an array or a list:
List<MapView> mapViews = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
MapView mapView = new MapView(getActivity(), i);
initialMapView(mapView);
mapViews.add(mapView);
}
(You can now use mapViews.get(...)
to get at each element later.)
EDIT: Note that both arrays and List<E>
are 0-based - so in the example above, you'd use mapViews.get(0)
to get the MapView
initialized with a second argument of 1
.
See more on this question at Stackoverflow