У меня в разметке очень много EditText'ов. Как находить и получить из них текст в цикле? Чтобы не писать слишком много кода

Как написать вот этот код по короче, без разницы в цикле или нет:


        //1Й ДЕНЬ
        editTextLesson11 = findViewById(R.id.editTextLesson11);
        editTextLesson12 = findViewById(R.id.editTextLesson12);
        editTextLesson13 = findViewById(R.id.editTextLesson13);
        editTextLesson14 = findViewById(R.id.editTextLesson14);
        editTextLesson15 = findViewById(R.id.editTextLesson15);
        editTextLesson16 = findViewById(R.id.editTextLesson16);
        editTextLesson17 = findViewById(R.id.editTextLesson17);

        editTextAuditores11 = findViewById(R.id.editTextAuditores11);
        editTextAuditores12 = findViewById(R.id.editTextAuditores12);
        editTextAuditores13 = findViewById(R.id.editTextAuditores13);
        editTextAuditores14 = findViewById(R.id.editTextAuditores14);
        editTextAuditores15 = findViewById(R.id.editTextAuditores15);
        editTextAuditores16 = findViewById(R.id.editTextAuditores16);
        editTextAuditores17 = findViewById(R.id.editTextAuditores17);

        editTextTeacher11 = findViewById(R.id.editTextTeacher11);
        editTextTeacher12 = findViewById(R.id.editTextTeacher12);
        editTextTeacher13 = findViewById(R.id.editTextTeacher13);
        editTextTeacher14 = findViewById(R.id.editTextTeacher14);
        editTextTeacher15 = findViewById(R.id.editTextTeacher15);
        editTextTeacher16 = findViewById(R.id.editTextTeacher16);
        editTextTeacher17 = findViewById(R.id.editTextTeacher17);

        //2Й ДЕНЬ
        editTextLesson21 = findViewById(R.id.editTextLesson21);
        editTextLesson22 = findViewById(R.id.editTextLesson22);
        editTextLesson23 = findViewById(R.id.editTextLesson23);
        editTextLesson24 = findViewById(R.id.editTextLesson24);
        editTextLesson25 = findViewById(R.id.editTextLesson25);
        editTextLesson26 = findViewById(R.id.editTextLesson26);
        editTextLesson27 = findViewById(R.id.editTextLesson27);

        editTextAuditores21 = findViewById(R.id.editTextAuditores21);
        editTextAuditores22 = findViewById(R.id.editTextAuditores22);
        editTextAuditores23 = findViewById(R.id.editTextAuditores23);
        editTextAuditores24 = findViewById(R.id.editTextAuditores24);
        editTextAuditores25 = findViewById(R.id.editTextAuditores25);
        editTextAuditores26 = findViewById(R.id.editTextAuditores26);
        editTextAuditores27 = findViewById(R.id.editTextAuditores27);

        editTextTeacher21 = findViewById(R.id.editTextTeacher21);
        editTextTeacher22 = findViewById(R.id.editTextTeacher22);
        editTextTeacher23 = findViewById(R.id.editTextTeacher23);
        editTextTeacher24 = findViewById(R.id.editTextTeacher24);
        editTextTeacher25 = findViewById(R.id.editTextTeacher25);
        editTextTeacher26 = findViewById(R.id.editTextTeacher26);
        editTextTeacher27 = findViewById(R.id.editTextTeacher27);

        //3Й ДЕНЬ
        editTextLesson31 = findViewById(R.id.editTextLesson31);
        editTextLesson32 = findViewById(R.id.editTextLesson32);
        editTextLesson33 = findViewById(R.id.editTextLesson33);
        editTextLesson34 = findViewById(R.id.editTextLesson34);
        editTextLesson35 = findViewById(R.id.editTextLesson35);
        editTextLesson36 = findViewById(R.id.editTextLesson36);
        editTextLesson37 = findViewById(R.id.editTextLesson37);

        editTextAuditores31 = findViewById(R.id.editTextAuditores31);
        editTextAuditores32 = findViewById(R.id.editTextAuditores32);
        editTextAuditores33 = findViewById(R.id.editTextAuditores33);
        editTextAuditores34 = findViewById(R.id.editTextAuditores34);
        editTextAuditores35 = findViewById(R.id.editTextAuditores35);
        editTextAuditores36 = findViewById(R.id.editTextAuditores36);
        editTextAuditores37 = findViewById(R.id.editTextAuditores37);

        editTextTeacher31 = findViewById(R.id.editTextTeacher31);
        editTextTeacher32 = findViewById(R.id.editTextTeacher32);
        editTextTeacher33 = findViewById(R.id.editTextTeacher33);
        editTextTeacher34 = findViewById(R.id.editTextTeacher34);
        editTextTeacher35 = findViewById(R.id.editTextTeacher35);
        editTextTeacher36 = findViewById(R.id.editTextTeacher36);
        editTextTeacher37 = findViewById(R.id.editTextTeacher37);
//Дальше ещё есть,но не буду захломлять

И вот этот код:

editTextLesson11.getText().toString();
editTextLesson12.getText().toString();
editTextLesson13.getText().toString();
editTextLesson14.getText().toString();
editTextLesson15.getText().toString();
editTextLesson16.getText().toString();
editTextLesson17.getText().toString();

editTextAuditores11.getText().toString();
editTextAuditores12.getText().toString();
editTextAuditores13.getText().toString();
editTextAuditores14.getText().toString();
editTextAuditores15.getText().toString();
editTextAuditores16.getText().toString();
editTextAuditores17.getText().toString();

editTextTeacher11.getText().toString();
editTextTeacher12.getText().toString();
editTextTeacher13.getText().toString();
editTextTeacher14.getText().toString();
editTextTeacher15.getText().toString();
editTextTeacher16.getText().toString();
editTextTeacher17.getText().toString();

editTextLesson21.getText().toString();
editTextLesson22.getText().toString();
editTextLesson23.getText().toString();
editTextLesson24.getText().toString();
editTextLesson25.getText().toString();
editTextLesson26.getText().toString();
editTextLesson27.getText().toString();

editTextAuditores21.getText().toString();
editTextAuditores22.getText().toString();
editTextAuditores23.getText().toString();
editTextAuditores24.getText().toString();
editTextAuditores25.getText().toString();
editTextAuditores26.getText().toString();
editTextAuditores27.getText().toString();

editTextTeacher21.getText().toString();
editTextTeacher22.getText().toString();
editTextTeacher23.getText().toString();
editTextTeacher24.getText().toString();
editTextTeacher25.getText().toString();
editTextTeacher26.getText().toString();
editTextTeacher27.getText().toString();

editTextLesson31.getText().toString();
editTextLesson32.getText().toString();
editTextLesson33.getText().toString();
editTextLesson34.getText().toString();
editTextLesson35.getText().toString();
editTextLesson36.getText().toString();
editTextLesson37.getText().toString();

editTextAuditores31.getText().toString();
editTextAuditores32.getText().toString();
editTextAuditores33.getText().toString();
editTextAuditores34.getText().toString();
editTextAuditores35.getText().toString();
editTextAuditores36.getText().toString();
editTextAuditores37.getText().toString();

editTextTeacher31.getText().toString();
editTextTeacher32.getText().toString();
editTextTeacher33.getText().toString();
editTextTeacher34.getText().toString();
editTextTeacher35.getText().toString();
editTextTeacher36.getText().toString();
editTextTeacher37.getText().toString();
//Дальше ещё есть,но не буду захломлять

Ответы (1 шт):

Автор решения: Style-7

Можно воспользоваться следующей конструкцией для перебора всех элементов:

for( int i = 0; i < amount; i++ ){
   int id = getResources().getIdentifier( "editTextLesson"+i, "id", getPackageName() );
   EditText edit_text = findViewById( id );
}
→ Ссылка