Is it possible to drive several Views of the same type into the list?

For example, like this:

private List<EditText> editTexts; editTexts.add((EditText) findViewById(R.id.ask1)); 
  • 3
    If the type is the same, why not? Just for starters, before adding, initialize editTexts for example like this: editTexts = new ArrayList<EditText>() - Chubatiy

1 answer 1

Yes, and this can sometimes be found, especially when the markup contains a large number of elements of the same type. In your case, this is EditText . I met with the ImageView .

To make the answer more complete, a small example:

 List<EditText> edList = new ArrayList<>(); int[] edIds = { R.id.ed_1, R.id.ed_2, R.id.ed_3, R.id.ed_4 ... R.id.ed_100 }; for (int i = 0; i < edIds.length; i++) edList.add((EditText) findViewById(edList[i])); 

This approach has its drawbacks not only in supporting the code, but it can be done that way.