Dusan Dimitrijevic Dusan Dimitrijevic - 1 year ago 41
Java Question

Modifying each object of a List in for each loop in Android

I'm fetching items from database. I have a few Edit Texts and I need to get text from these editable texts and pass that text, update too objects of a List I get from database. I'm getting values from Edit Texts. This is how it looks in code:

String getNasteMonday = etNasteMonday != null ? etNasteMonday.getText().toString() : null;
String getInsulinMondayBeforeBreak = etInsulinMondayBeforeBreak != null ? etInsulinMondayBeforeBreak.getText().toString() : null;
List<Day> days = mDatabase.getAllDaysByWeek(week.getTitle());
double nasteValue = convertStringToDouble(getNasteMonday);
double insulinBeforeBreakValue = convertStringToDouble(getInsulinMondayBeforeBreak);

for (Day day : days) {

In this case I have three tables in database, one for storing days, and one for storing weeks and one for storing days by weeks. I'm fetching data in this case by week name and every Week object has 7 days and I need to store different values for each day in that Week. Now, what I'm getting is that in this foreach loop I'm storing the same value for all 7 days and I don't want that. I want to do something like this for example:

for (Day day1 : days) {
for (Day day2 : days) {


How to show text in EditText when i have fetched data from database:

I should set text from an array of Strings.

List<String> values = new ArrayList<>();
int[] ids = new int[]{R.id.et_naste_monday, R.id.et_insulin_monday_before_breakf, R.id.et_posle_dorucka_monday, R.id.et_pre_rucka_moday,
R.id.et_insulin_monday_pre_rucka, R.id.et_posle_rucka_monday, R.id.et_pre_vecere_moday, R.id.et_insulin_monday_pre_vecere, R.id.et_posle_vecere_monday,
R.id.et_pred_spavanje_moday, R.id.et_insulin_monday_pred_spavanje};
List<Day> days = mDatabase.getAllDaysByWeek(week.getTitle());

int count = 0;

for (int id : ids) {
EditText t = (EditText) findViewById(id);
applyChangedEditTextColor(false, values, t);
// Here i should implement some logic like and apply for all edittexts
// for (Day day : days) {
// }

Answer Source

Since you have a lot of EditText to use. I would create some Arrays to store the instance an recover those in a loop.

EditText[] nasteEdit = new EditText[]{ /* your seven instance of editText for naste value in correct order */};
List<Day> days = mDatabase.getAllDaysByWeek(week.getTitle());

int cntDays = 0;
for (Day day : days) {
    String getNasteMonday = nasteEdit[cntDays] != null ? nasteEdit[cntDays].getText().toString() : null;
    double nasteValue = convertStringToDouble(getNasteMonday);



During the loop, I get the correct EditText and create the value. I keep you for-iterative loop here and use an external counter but this should work.

You just need to do this for every attributes Day have and this will work.

The array need to have the instance of the EditText, so don't waste you time to get the text of each ;)

Hope this will work, I can't test it write now.