-1

I have 50 textviews.can i avoid initializing all the textviews like this.Is there is any code to initialize all these widgets automatically

t1 = (TextView) myFragmentView.findViewById(R.id.tvone);
t2 = (TextView) myFragmentView.findViewById(R.id.tvtwo);
t3 = (TextView) myFragmentView.findViewById(R.id.tvthree);
t4 = (TextView) myFragmentView.findViewById(R.id.tvfour);
t5 = (TextView) myFragmentView.findViewById(R.id.tvfive);
t6 = (TextView) myFragmentView.findViewById(R.id.tvsix);
t50 = (TextView) myFragmentView.findViewById(R.id.tvfifty);
Teja
  • 837
  • 3
  • 14
  • 24

2 Answers2

0

you can get all EditTexts in your View by this method :

Method to get all EditTexts in a View

also you can create a map that contains "ID ,Textview"

    HashMap<Integer,EditText> myEditTextList = new HashMap<Integer,EditText>();

    for( int i = 0; i < myLayout.getChildCount(); i++ )
        if( myLayout.getChildAt( i ) instanceof EditText )
            myEditTextList.put(  ((EditText) myLayout.getChildAt( i )).getId() , 
                   ((EditText) myLayout.getChildAt( i )));
Community
  • 1
  • 1
mehd azizi
  • 594
  • 1
  • 5
  • 16
0

You can initialize all TextViews by renaming them to "tv1 ... tv50", and use this code:

int ressourceId;
int tv_number = 50;
TextView[] tv = new TextView[tv_number];

for(int i = 0; i < tv_number; i++) {
    ressourceId = getResources().getIdentifier("tv" + (i + 1), "id", context.getPackageName());
    tv[i] = (TextView) myFragmentView.findViewById(ressourceId);
}
FKH
  • 13
  • 5