I have 10 textView. And for these 10 fields, I create 10 variables of type String. Is it possible to put all this into a cycle so that each new text is in each new textView?

  • one
    stackoverflow.com/questions/30865668/… - SorryForMyEnglish 2:51 pm
  • Thank you for what you need! - Uliyan Romanov
  • @SorryForMyEnglish, if you translate your message and add it as an answer, more people will be able to use it - Timofei Bondarev
  • it's better not to do so) - SorryForMyEnglish
  • I think reflection in solving such a banal task is an unnecessary and unjustified decision. - pavlofff

2 answers 2

In general, if you have such a markup in the leyauta:

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView2"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView3"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView4"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView5"/> 

Perhaps the best way to collect all this in a bunch and iterate will be something like this:

 List<TextView> textViewList = new ArrayList<>(); for(int i = 1; i < textViewCount + 1; i++){ int id = getResources().getIdentifier("textView" + i, "id", this.getPackageName()); TextView textViewItem = (TextView)findViewById(id); textViewItem.setTest("Новый текст" + i) textViewList.add(textViewItem);//кладем в колекцию если вы собираетесь еще раз менять текст } 

Collecting a view into a collection is not necessary if the action is disposable. textViewCount can be hardcoded, but it can also be calculated by calling the getChildCount() method on the container if there are other views in the container, you need to loop through the getChildAt(index) child method and check if(child instaceof TextView)

and still not to be dependent on id you can do this:

 List<TextView> textViewList = new ArrayList<>(); for(int i = 0; i < container.getChildCount(); i++){ View child = container.getChildAt(i); if(child instanceof TextView){ textViewList.add((TextView) child); } } 

My response to the link does the same thing only through reflex, provided that all text fields are declared in the activation class and initialized.

  • You forgot that we need to line up the lines there, and not pack them into the collection. If the action is a one-time action, then it is more logical to immediately reject during the iteration, rather than add up for another iteration. True, the author did not say anything about this - out of context, I understand that the action is a one-time action. And yes, perhaps the most optimal way to go through a handful of widgets - pavlofff
  • Tochnyak. wrote, wrote and did not answer the question) we 'll fix it - SorryForMyEnglish
 ArrayList<TextView> textViews = new ArrayList<>(); textViews.add((TextView)findViewById(R.id.text1)); textViews.add((TextView)findViewById(R.id.text2)); ... for(int i = 0; i < textViews.size(); i++) { textViews.get(i).setText(strings.get(i)); } 
  • one
    Is there anything that you have a TextView type of wife and are you trying to shove the View type? :-) - SorryForMyEnglish
  • @SorryForMyEnglish I bow before the Lord of Andoid. I promise to fix it in the future! - iamtihonov
  • I hope he sees it) - SorryForMyEnglish