glaukon
Chapter 3: Java - Part 11
Since this is a tip calculator, it might be a good time to finally start getting into the math part. For now, let's work on getting the bill splitting portion of the app to add up all the orders of a particular Diner properly. To do this, we're going to want to listen to all the order EditTexts to see when they lose focus (which implies the user just changed them). When one loses focus, we want to update the TextView at the bottom of the app to show the total bill for a particular Diner, updated with the change the user just made. So the first thing we want to do is add listeners to a bunch of EditTexts. Give amount1of1 an OnFocusChangeListener in onCreate.
amount1of1.setOnFocusChangeListener(this);
Then go to the code for making a new Diner, and make sure every new Diner's EditText for its first order also has an OnFocusChangeListener.
et2.setOnFocusChangeListener(this);
Finally, set an OnFocusChangeListener to new order EditTexts every time the user adds a new order.
newOrder.setOnClickListener(this);
Now that we're listening to all the EditTexts we need to, let's program what happens when these things lose focus. Go to the onFocusChange code block, and add this inside the for loop that runs through all the Diners, and under the if block that checks if it was a Diner's name that changed:

else {

for (int j = 0; j < dinerList.get(i).orderList.size(); j++) {

if (v == dinerList.get(i).orderList.get(j) && hasFocus == false) {

dinerList.get(i).updateTotal();

}

}

}

Here, we have another for block within a for block. When Android detects that something that MainActivity is listening to loses or gains focus, it runs through all the Diners in dinerList. If it was a name EditText that triggered the code, the code we wrote earlier about name changing runs. Otherwise (i.e. else), we want to run this new code here. This new code has a for loop that runs through every order in a particular Diner's orderList. For each order EditText, we check if it is equal to the view (v) that triggered onFocusChange. We also check if it lost focus, rather than gained it. If both of these are true, we run that Diner's updateTotal method. Again, the for loop nesting means we're going through every order (2nd loop) of every Diner (1st loop). This way, we're checking every single order EditText to see if any of them gained or lost focus.