Adding Items to TodoList7:10 with Jamie Huson
Add subscriptions to your TodoList with RxJava.
Next let's modify the listener we used to add items to the to do list. 0:00 So we can see here, in our main activity, we had a call to find you by ID, 0:05 setOnClickListener, and in that callback we'd get our onClick. 0:10 The first thing we're gonna do is we need to get the actual text 0:14 from our addInput button which is an edit text. 0:18 Then we wanna make sure it's not empty we don't want to add empty or 0:23 no items essentially to our list. 0:27 So we needed to check that and then we added the actual item to our list. 0:30 Finally we clear the input in the editText and 0:36 we dismissed the keyboard using these two lines here. 0:40 So we're gonna use this instead of 0:43 onClickListener call back we wanna convert this into be an observable. 0:47 So we are in observable it's going to emit items. 0:52 So what we essentially want here is we want every time an item should be added 0:55 we want to a emit it out. 1:00 And so we're gonna transform this listener and these checks 1:02 into a set of callbacks as an observable and then we're gonna subscribe to that. 1:07 And whenever we have a valid item to add to our list, 1:11 we'll be able to add it to our list just like we did before. 1:14 So let's start by doing that. 1:17 The first thing I wanna do is we're gonna use the Rx bindinds library 1:20 that we have added as a dependency. 1:24 And there's something in there called RxView. 1:26 And RXView is a helper for 1:30 handling any interactions with base view types so just the basic view class. 1:33 And so one of those things is handling clicks. 1:38 So we can see here that RxView.clicks takes in a view and 1:42 it returns back an observable. 1:46 And we can subscribe to that observable and notice it's of type void. 1:48 That's because the subscription we get is going to emit an item every time that 1:53 view is tapped or clicked, similar to the onClickListener. 1:59 So let's go ahead and use the same button we were using before. 2:04 And it's helpful to know when things are tapped but we also can take that and 2:13 then before we get to the subscriber, 2:18 before we get to the observer, which we pass into the subscribe method, 2:21 we're gonna first transform this click into the actual data we need. 2:25 So the actual data we need in the end is a to do item to add to our list. 2:30 So the first thing you wanna do is first every time a click happens, let's map it. 2:36 So we can call .map and 2:43 RxJava is really great because it uses this nice syntax where we can keep 2:45 calling different operators after we create the initial observable. 2:50 So here I'm adding a map operator and what we want here is a new function. 2:55 And what we want here is we're 2:58 gonna transform our click into a string. 3:03 And that string is gonna be the text. 3:09 So just like down here where we were calling addInput.getText toString. 3:11 What we're gonna do is we're going to have a call and 3:17 for every click that's the void that happens for 3:25 every click we're gonna return back the add input get text to string. 3:27 So now, if I were to subscribe at this point, 3:38 I would be getting items that are strings. 3:42 I would be getting the text that are strings every time the button is clicked. 3:47 But we also are doing this check to see if it's empty or not. 3:51 So we can also add an additional operator to check for 3:55 that as well and that we can use filter for. 3:59 So here I’m gonna to provide another function and what this functions gonna do 4:03 is takes in our string and then it returns a Boolean. 4:08 And what the Boolean means is if it's true we're going to emit this item. 4:12 If it's not true this item won't be emitted. 4:17 So what we're essentially doing is checking here, and 4:21 the thing we're checking is if this is empty or not. 4:24 So let's go ahead and do this check. 4:28 So all we have to do is say, the string ass that comes in so the string ass, 4:34 is going to be emitted through the map function. 4:39 That's gonna be a text from the input box. 4:42 And in here if it's not empty, we're gonna go ahead and 4:44 allow it to be emitted from our observable. 4:48 And that's exactly what we want. 4:52 So finally we can subscribe to this. 4:54 And when we subscribe, we're gonna go ahead and provide an action again. 4:58 We don't care about on completed or on erroring this case. 5:02 We just care about when those items are emitted because they're valid items to be 5:07 added to our list. 5:11 So when an item is finally admitted, we'll get it in this call and 5:16 what we can do is exactly what we were doing before. 5:21 And now we can delete this entire findViewById setup listener business 5:34 we were doing before. 5:39 And we can see that, using RxJava, and these nice wrappers in RxView. 5:41 We can really build out functionality so that our subscriber here 5:48 in our subscription only receives calls when it has valid data. 5:55 We don't have to do additional error checking here, and 6:00 this is really helpful because while we're inlining, the subscriber right here. 6:02 It could be the case that there's a totally different class or 6:08 object, that is going to be the observer. 6:11 And In that case, you won't have to worry about doing any of the is empty checks, or 6:15 getting the text. 6:19 It is only going to get a call 6:21 when a string is ready to be added as a new TodoList. 6:23 So it really does simplify things here. 6:27 And what we'll also see is that, because we added our subscription and 6:30 our subscriber to our TodoList and our adapter. 6:34 Last time, every time we add a item here to our list 6:38 our adaptor is automatically gonna be updated with this new item in the list. 6:41 So that's just gonna happen for us, 6:45 we don't have to worry about it because this subscription is set up. 6:47 So here we took our button, 6:51 we transformed it into an observable when clicks or taps happen. 6:54 And we filter that data, and made sure that we only emit items 6:58 when they're valid, and when we do emit items, finally can add those to our list. 7:03
You need to sign up for Treehouse in order to download course files.Sign up