I want to minimise the code by using for loop with TextView objects a,b,c,d,e as elements of an array. Both for findViewById and setOnClickListener implementations. Any actionable walkthrough for this particular coding is really appreciated!^__^
The following is how I usually do the TextView implementations. But I am tired of writing so many lines needlessly.
TextView a,b,c,d,e;
a=(TextView)findViewById(R.id.A); b=(TextView)findViewById(R.id.B); c=(TextView)findViewById(R.id.C); d=(TextView)findViewById(R.id.D); e=(TextView)findViewById(R.id.E); a.setOnClickListener(this); b.setOnClickListener(this); c.setOnClickListener(this); d.setOnClickListener(this); e.setOnClickListener(this);My question is if I can use a loop to set all the already initialized TextView objects to call setOnClickListener() without any trouble as shown below:
TextView a,b,c,d,e; a=(TextView)findViewById(R.id.A); b=(TextView)findViewById(R.id.B); c=(TextView)findViewById(R.id.C); d=(TextView)findViewById(R.id.D); e=(TextView)findViewById(R.id.E);TextView[] textViews = {a, b, c, d, e};
for (int count = 0; count < textViews.length; count++) { textViews[count].setOnClickListener(this); }
**
All I want to know is how to initialize the TextView objects a,b,c,d,e in the same way, I showed to setOnClickListener for them? Something like this,
TextView[] textViews = {a, b, c, d, e}; int[] textViewIds = {R.id.a, R.id.b, R.id.c, R.id.d, R.id.e}; for (int count = 0; count < textViews.length; count++) { textViews[count] = (TextView)findViewById(textViewIds[count]); }
**