I have a view with several TextViews, EditViews, and a Button. I want to iterate through all the items and get the text value of each item. I can't just go through each by name because depending on what the user did on the screen before, depends on what items show up on the screen. And, there can be a different number of items every time. I create the items dynamically and set the text of each textview, but the user will enter info into the editview. So I need to know not only the text from the textview, but the text the user put into the editview and I need to make sure that the user input goers to the right textview. So if I have a textview that reads "2x2" and the user puts in "4", I need to make sure I know 4 goes to the 2x2 textview. I don't know where to start looking so I haven't tried anything. If someone can point me in the right direction, that would be fine. By the way, this is not homework. It's still the same app I'm working on that has been destroying me for weeks. Thanks.
Asked
Active
Viewed 146 times
1 Answers
0
You can iterate through the children and then check for class types.
// you need the ID of the layout - in this example the ID is myLayout
// or you get the layout object instance otherwise
LinearLayout layoutMyLayout = (LinearLayout) findViewById(R.id.myLayout);
if (layoutMyLayout != null)
{
for (int i = 0; i < layoutMyLayout.getChildCount(); x++)
{
View viewChild = layoutMyLayout.getChildAt(i);
if (viewChild instanceof EditText)
{
// get text from edit text
String text = ((EditText)viewChild).getText().toString();
//TODO: add rest of the logic
} else if(viewChild instanceof TextView) {
// get text from text view
String text = ((TextView)viewChild).getText().toString();
//TODO: add rest of the logic
}
}
You only need the instance and from there you have those methods posted up. Pretty straight-forward.

Jernej K
- 1,602
- 2
- 25
- 38
-
I changed the if condition to rather use `instanceof` then comparing class name. That is also the reason I removed the link, because I think this is even better. – Jernej K Mar 24 '16 at 00:12
-
-
For the most part yes. But after reading a ton of stuff, I think I'm going to go another way with this. Since I create the textviews and edittexts on the fly, I'm just going to name them the whatever the value of the text is. So if the value of text in a textview "Jeff", then the edittext id will be "Jeff". or more over R.id.Jeff. My problem is trying to figure out how to build the text.setId() part of the statement. Sorry, I rambled, but, yes, I did use it and it did work for me. Thank you. – vbneil54 Mar 25 '16 at 01:13