Office Hours — Today, February 9

Thursday, February 7

Feb 9
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
4:00 PM
Anshul V.
has entered the room
4:05 PM
Mark M.
hello, Anshul!
how can I help you today?
Anshul V.
Hi Mark, how are you doing?
Mark M.
OK, and you?
Anshul V.
I am doing fine thank you!
I wanted to talk about the SO bump question we talked about yesterday
Mark M.
Anshul V.
yes
Mark M.
OK, same post, different URLs :-)
"So at the end when the user presses Continue, I get a List<InputData>, one for each fragment user created" -- isn't this a matter of the activity iterating over the fragments and calling giveMeMyInputDataKThxBye() on each
then assembling that into a List<InputData>?
Anshul V.
Well, yes, but I am not sure how to do that. I think I have got validation and passing the data confused and can't seem to get either
Mark M.
your EditProfilePagerAdapter has a List<Fragment>
4:10 PM
Mark M.
change that to be a List<EditProfileFragment>
then do a for loop over it
for (EditProfileFragment fragment in fragments) { // TODO }
where the TODO calls some method on the fragment to get the InputData and adds it to an ArrayList
Anshul V.
okay, but how to I access that data in the activity? I have not linked the List<EditProfileFragment> in EditProfilePagerAdapter
Mark M.
write a method on EditProfilePagerAdapter
call it collectInputData() or something
it does the for loop over the List<EditProfileFragment> and returns the List<InputData>
and your activity calls collectInputData() when the user clicks the Continue button
Anshul V.
hmm, so the InputData that is present in each fragment gets added to the list. I guess I make the InputData in each fragment accessible to the EditProfilePagerAdapter then?
Mark M.
that was my proposed giveMeMyInputDataKThxBye() method from earlier
though you might want to use a simpler name :-)
4:15 PM
Anshul V.
Haha..like it, so each fragment has a public giveMeMyInputDataKThxBye(), which it sends to EditProfilePagerAdapter when activity calls collectInputData() on continue button press?
Mark M.
"sends" implies a different flow than I'm thinking -- "returns" would be what I would use
but otherwise, yes
Anshul V.
hmm, got it.
4:20 PM
Anshul V.
Now where does the validation fits in the picture? I want to validate a couple of input fields of the EditProfileFragment and want to raise error if that field is not filled? Since there can be many EditProfileFragment added via the Add Fragment button in Activity, I was thinking each time the Add Fragment button is clicked, that current fragments data gets validated and when that is correct, then only the data is "returned" to the EditProfilePagerAdapter? Am I going in the right direction?
Mark M.
you would need to validate both when the user clicks "Add" and "Continue", as otherwise you will not validate the last fragment
to do that, have a validate() method on the fragment that performs the validation and updates it UI, returning a boolean indicating whether the validation succeeded
call that as part of processing Add and Continue clicks, not proceeding if validate() returns false
you can find out the index of the current page from the ViewPager, and use that to look up the current fragment in your adapter
4:25 PM
Anshul V.
Hmm, I did not think about the last fragment. So, the flow would be -> When user clicks +Add/Continue, the pager adapter requests data for "current" fragment, and upon that request, the data before being returned to Adapter, is validated, if anything wrong, error is raised and data is not sent, if right the data is sent to adapter, which adds it to a List<InputData> which can later be inputted in collectInputData() call by activity.
Mark M.
something like that, yes
the fact that you are using a ViewPager makes things more complicated, as the user could have a valid fragment, add another one, go back to the first one, make a mistake there, swipe back to the last one, then click Continue
your Continue validation probably needs to check all the fragments, and collect all the data at that point
4:30 PM
Anshul V.
Hmm, that makes sense. let's say if there is an error in say fragment 3, then pager adapter would load fragment 3 as a current fragment (on continue press) and communicate with fragment about the field that is not validated (still not sure how this would happen) and then the fragment would do setError() on that field of fragment 3.
Mark M.
well, my gut instinct is that the fragments should do the validation themselves -- I'm not quite certain why you are having the activity do it
so, on a Continue press, you would iterate over the fragments and call validate() -- if one returns false, you switch the pager back to that fragment
if none return false, then all the data is valid, so you collect the data from each of the fragments and continue on your merry way
4:35 PM
Anshul V.
Ah, so validate() would be inside giveMeMyInputDataKThxBye(), so before data is returned to the collectInputData() in PagerAdapter, it is already validated, each time.
Mark M.
that's a possibility, where giveMeMyInputDataKThxBye() returned null to indicate invalid data
I had envisioned validate() and giveMeMyInputDataKThxBye() being separate, but either way could work
Anshul V.
okay, if validate() and giveMeMyInputDataKThxBye() are different, what would be the trigger to call the validate()? Since there is no button/event inside fragment where we know that now user has entered the data and it can be validated.
Mark M.
at minimum, you would call validate() when the user clicked Continue
you could also call validate() when the user clicked Add, to confirm that the current fragment is OK, though due to the pager, you can't rely on it *staying* valid if the user goes back and make changes
4:40 PM
Mark M.
but, on a Continue click, you would:
1. iterate over the fragments, calling validate() on each
2. if a validate() call returns false, switch the pager to that fragment and you're done for now
3. if all validate() calls return true, you iterate over the fragments and call giveMeMyInputDataKThxBye() to build your List of results, and you continue from there
if giveMeMyInputDataKThxBye() is cheap, and you want to just have one iteration loop and use a null return value to signal invalid data, so you do not need a separate validate() method, you could do that
Anshul V.
okay, I think I got it, both validate() and giveMeMyInputDataKThxBye() are going to be called by PagerAdapter, on Continue button press in activity
Mark M.
yes
(BTW, sorry about the delay there -- my computer froze, so I needed to reboot)
4:45 PM
Anshul V.
No problem, I didn't notice it..
okay, Mark. I should be able to make this work. Thank you very much for your time and input, I appreciate it.
Mark M.
you're welcome!
4:50 PM
Anshul V.
I would be coming again to the office hours, I have a lot of questions regarding the app architecture where I think it can be improved. I have just started on my Android Development journey and I thank you for making such amazing books!
Have a good weekend!
Mark M.
you too!
and thanks for the kind words!
5:00 PM
Mark M.
that's a wrap for today's chat
the transcript will be posted on https://commonsware.com/office-hours/ shortly
the next chat is Tuesday at 7:30pm US Eastern
have a pleasant day!
Anshul V.
has left the room
Mark M.
turned off guest access
5:20 PM
Mark M.
has left the room
5:50 PM
Mark M.
has entered the room

Thursday, February 7

 

Office Hours

People in this transcript

  • Anshul Vyas
  • Mark Murphy