Android: array of textviews
I am making an app in which I want to change the text of textviews from an array of strings. For that I need to make the array of textviews.How to do that?? Can anyo开发者_运维问答ne help me over this
You can create TextViews like this:
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
}
May be its useful for you i use button array so i am gussing textview work like that:
TextView[ ][ ] _txt;
_txt = new TextView[_dimension][_dimension]; // _dimension = 5 what you want
_txt[0][0] = (TextView) findViewById(R.id.text1);
_txt[0][1] = (TextView) findViewById(R.id.text2);
and more...
If you want large number of textviews, in that case to avoid OutofBound exception use following code
LinearLayout parent = new LinearLayout(this);
TextView textView;
for( i = 0; i < count; i++) {
textView = new TextView(this);
textView.setTag(""+i);// setting tag with index i
parent.addView(textView);
}
int len=parent.getChildCount();
int j = 0;
int requiredPosition = 5;
while(j<len) {
TextView tempTextView =((TextView)parent.getChildAt(i));
if( tempTextView.getTag().equals(""+requiredPosition)){
//Perform required operation
//tempTextView.setText("");
}
j++;
}
You can do achieve that with something like this:
int textvwCount = 20;//number of textview you want to use in an array
TextView[] arrTxtView = new TextView[textvwCount ]; // declare and assign array length of text views.
for(int i = 0; i < textViewCount; i++) { // iterate over all array items and assign them text.
Textview txtCnt = new TextView(this);
txtCnt .settext(i);
textViewArray[i] =txtCnt ;
}
精彩评论