powerlanguage
powerlanguage

Reputation: 1

android TextView arrays

I am making a word game in which each a user has multiple guesses, each one made up of multiple TextViews. So far my code reads:

TextView[] guess1 = new TextView[numTextViews];
guess1[0] = (TextView) findViewById(R.id.Guess1_1);
guess1[1] = (TextView) findViewById(R.id.Guess1_2);
guess1[2] = (TextView) findViewById(R.id.Guess1_3);
guess1[3] = (TextView) findViewById(R.id.Guess1_4);
guess1[4] = (TextView) findViewById(R.id.Guess1_5);

with the xml looking like:

<TextView
    android:id="@+id/Guess1_1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:text="@string/guessChar" />...

which repeats with android:id= changing.

I am going to be repeating myself if I type out TextView[] guess2 and all its elements.

Upvotes: 0

Views: 6443

Answers (3)

Voicu
Voicu

Reputation: 17850

This is how you can iterate through your views without the use of ids in repetitive code:

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_containing_textviews);
for (int i = 0; i < ll.getChildCount(); i++) {
    if (ll.getChildAt(i).getClass() == TextView.class) {
         guess1[i] = (TextView)ll.getChildAt(i);
    }
}

Make sure to tweak this in case you have non-TextView views since the i index will not be consecutive in that case. You can use another counter just for the TextViews.

Now if your layout has only TextViews, you don't even need an array. You can use that layout as a container/array the way it's used in the snipped above.

Upvotes: 3

android developer
android developer

Reputation: 116332

You could either create the textViews programmatically (and use inflate if you wish to use some xml too), or you could use the getIdentifier method , for example:

private static final String ID_FORMAT="Guess1_%d";
...
for(int i=0;i<10;++i)
  {
  String id=String.format(FORMAT,i);
  TextView tv = (TextView) findViewById(getResources().getIdentifier(id, "id", getPackageName()));
  //...
  }

same goes if you wish to do a loop within a loop.

If the layout has a lot of views, I would suggest using an adapterView (listView,gridView,...) instead, and avoid creation of so many views (either programmatically or by xml).

Upvotes: 0

Elior
Elior

Reputation: 3266

Do you know what is the amount of guesses for each text view?

I would suggest you to use reflection

Class clazz = R.id.class; // get the R class
Field f = clazz.getField("Guess1_" + "1");
int id = f.getInt(null);  // pass in null, since field is a static field.
TextView currcell = (TextView) findViewById(id); 

in this case it will bring the Guess1_1

for you case:

 for (int i =0; i < numTextViews; i++)
 {
     Class clazz = R.id.class;
     Field f = clazz.getField("Guess1_" + Integer.toString(i+1));
     int id = f.getInt(null);
     guess[i] = (TextView)findViewById(id);
 }

but this only bring you the first array of Guess1 you need to convert it to generic code.. so some problems can be occur.. so read it with the xml as you have right now would be the easiest way..

Edit:

If the all textView have the same attributes you can also create it programmatically

LinearLayout view = new LinearLayout(this); // create new linear layout
view.setOrientation(LinearLayout.HORIZONTAL); // optional.. so the 
                                              // view will be horizontaly

view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                         LinearLayout.LayoutParams.WRAP_CONTENT)); // set the layout           
                                                                   // height and width

 for (int i = 0; i < numOf ; i ++)                                      
 {                       
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.WRAP_CONTENT, 
               LinearLayout.LayoutParams.WRAP_CONTENT);

     guess[i] = new TextView();
     guess[i].setLayoutParams(lp);
     guess[i].setID(i+1);
}

Upvotes: 0

Related Questions