For example, 1 lavout may contain a different (previously unknown) amount of textview. Is there a way to extract text from them and put it in a variable?
1 answer
- Cycle through all the children in the container.
- Check if the child is
TextView
If yes, remove the text.
String text = ""; for(int i = 0; i<someContainer.getChildCount(); i++) { if(someContainer.getChildAt(i) instanceof TextView) { text += ((TextView)someContainer.getChildAt(i)).getText().toString(); } }
- I tried the variant you proposed, + passed text into textview2, it turns out that the contents of only the last textview from which the text was extracted are transferred to textview2. Therefore, the text variable stores only 1 text. - Elena Chchchchchchch
- @ ElenaChchchchchchch, that's right. I wrote exactly what you asked for - for each text field in the container, an object of the string type is created and the text from the text field is placed into it. To get the text of all the fields in one variable, you do not need to create a new variable each time, but to supplement the existing one. See updated code. - Yuriy SPb ♦
|
TextView
... but at some point it’s probably not particularly correct) - ermak0ffinstanceof
speed and if there is a need for it, then the application architecture is most likely wrong" .... but this is just "tale")) - ermak0fftry
wrap it in atry
))) - YuriiSPb ♦TextView
dynamically, it is easier to store links to them, and not to sort through the entire root layout - ermak0ff