Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a view's elements

Tags:

android

forms

I have a view with radios, inputs and a button and when I click it, I want to check that all inputs contain information. How can I iterate through the view's elements in the activity and check if every textview meets the aforementioned requirement ? Thanks.

like image 867
xain Avatar asked Jan 26 '11 20:01

xain


People also ask

How to iterate over all elements in Dom?

Run a loop and access them one by one by indexing (Eg. el [i] for i th element). Example: This example implements the above approach. + "iterate over all DOM elements.";

How do you iterate through a list in V for?

Using v-for with Range The built-in v-for directive allows us to loop through items. We can use a range in the v-for directive to iterate a specified number of times. Let’s replace the contents of our <div> with an unordered list that repeats list items 15 times:

How to iterate a specified number of times in V-for directive?

We can use a range in the v-for directive to iterate a specified number of times. Let’s replace the contents of our <div> with an unordered list that repeats list items 15 times: This will result in an unordered list with numbers 1 to 15.

How do I loop over the items in a shoppingitems array?

We can loop over the items in a shoppingItems array from the data model. This can be accomplished by adding the v-for directive in the element that should be repeated. Let’s modify the lines in data () so it returns a shoppingItems array with objects:


2 Answers

I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "layout"):

LinearLayout layout = (LinearLayout)findViewById(R.id.layout); boolean success = formIsValid(layout);  public boolean formIsValid(LinearLayout layout) {     for (int i = 0; i < layout.getChildCount(); i++) {         View v = layout.getChildAt(i);         if (v instanceof EditText) {             //validate your EditText here         } else if (v instanceof RadioButton) {             //validate RadioButton         } //etc. If it fails anywhere, just return false.     }     return true; } 
like image 166
Kevin Coppock Avatar answered Sep 20 '22 02:09

Kevin Coppock


To apply the method by kcoppock recursively, you can change it to this:

private void loopViews(ViewGroup view) {     for (int i = 0; i < view.getChildCount(); i++) {         View v = view.getChildAt(i);          if (v instanceof EditText) {             // Do something          } else if (v instanceof ViewGroup) {              this.loopViews((ViewGroup) v);         }     } }  
like image 43
Kalimah Avatar answered Sep 17 '22 02:09

Kalimah