You don't have to use reflection, you can use the package manager's
APIs to create ids from strings (and the other way around.)

On Fri, Apr 24, 2009 at 5:54 AM, MrSnowflake <[email protected]> wrote:
>
> If you really want to use a for loop (for maintenance point of view,
> somewhat, as TextView1...10 aren't very clear) you need to use
> reflection which is a lot of work for this and it's slow.
> For your purpose you should consider creating your layout fully in
> code.
>
> On 24 apr, 14:30, Mark Murphy <[email protected]> wrote:
>> Zhubham wrote:
>> > Hi Experts,
>>
>> > I am trying to optimize the following code:
>>
>> >                           textview[0]=(TextView)findViewById
>> > (R.id.TextView1);
>> >              textview[1]=(TextView)findViewById(R.id.TextView2);
>> >              textview[2]=(TextView)findViewById(R.id.TextView3);
>> >              textview[3]=(TextView)findViewById(R.id.TextView4);
>> >              textview[4]=(TextView)findViewById(R.id.TextView5);
>> >              textview[5]=(TextView)findViewById(R.id.TextView6);
>> >              textview[6]=(TextView)findViewById(R.id.TextView7);
>> >              textview[7]=(TextView)findViewById(R.id.TextView8);
>> >              textview[8]=(TextView)findViewById(R.id.TextView9);
>> >              textview[9]=(TextView)findViewById(R.id.TextView10);
>>
>> > using:
>>
>> > int j;
>> > for(int i=0;i<10;i++)
>> > {
>> >                                    j = i+1;
>> >                       textViewId = "TextView"+j;
>> >                       textview[i] = 
>> > (TextView)findViewById(R.id.textViewId);//
>> > error textViewId not recognized
>> > }
>>
>> > but this is giving error at the last line.. Can you please help me to
>> > come out of this.
>>
>> 1. There is nothing named R.id.textViewId in your layout, in all likelihood.
>>
>> 2. Putting things in loops does not optimize the code, from an execution
>> speed standpoint.
>>
>> 3. You cannot dynamically construct the name of a widget ID in the
>> manner you are trying.
>>
>> --
>> Mark Murphy (a Commons 
>> Guy)http://commonsware.com|http://twitter.com/commonsguy
>>
>> _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
> >
>



-- 
Romain Guy
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to