Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
To learn RxJava we’re revisiting an app you’ve probably built before: a Todo list. We are including an implementation of a simple Todo app built without the use of RxJava.
-
0:00
To learn RX Java we're revisiting an app you've probably built before.
-
0:04
A to do list.
-
0:06
I've included an implementation of a simple to do app
-
0:09
built without the use of RX Java.
-
0:12
Let's take a look at the code and see what's going on.
-
0:15
Here we can see that we have one activity,
-
0:18
the main activity We have a data model object.
-
0:22
A to do is just as a description and
-
0:25
a boolean to indicate if this to do item is completed.
-
0:28
We have a to do list.
-
0:32
So a to do list, is a list of to do's and this to do listener.
-
0:37
Let's go to the to do listener,
-
0:39
that's a simple interface it has a method called on to do is change.
-
0:43
So, if the to do list changes, something is gonna listen for
-
0:46
changes to that and update that you are.
-
0:50
So our to do list keeps our items and whenever we add or
-
0:55
remove an item from our to do list, the listener will be called.
-
1:00
And also some other helpful methods to set the listener up, to get the number of
-
1:04
items in our list, and to get a specific to do from our list as well.
-
1:10
You'll notice that the to do list also reads data in from Json and writes it
-
1:16
to Json in the to string, which is just helpful for serializing this data.
-
1:23
If we need to we can also see that there is something called a to do list filter so
-
1:27
this is a class.
-
1:29
And it implements the to do listener so that means this receives a call back.
-
1:33
When the to do list changes in some way.
-
1:37
And here we can see at some sort of filter.
-
1:39
It describes What to dos are being shown.
-
1:42
So, all.
-
1:43
That's all items, whether they are completed or not complete.
-
1:47
Incomplete is a one, and completed is a two.
-
1:51
And there's basically a mode, a filter mode.
-
1:53
So, by default, we want to show all the items.
-
1:56
And this filter class is responsible for.
-
2:01
Filtering the items out.
-
2:02
So if we're only looking at the incomplete items.
-
2:05
Then we're going to take the to do list.
-
2:07
And we're going to compile just the ones that are only incomplete.
-
2:11
And the same with complete.
-
2:12
We'll take the items.
-
2:13
And just compile a list of just the items that are completed, and return those.
-
2:18
You'll notice that if it's all, we're just going to return the list, itself.
-
2:23
So there's a to do list filter.
-
2:26
We can see that there is an adapter to display these in our cycler view.
-
2:30
And this also listens for changes.
-
2:32
This it to do completed change a listener here.
-
2:37
And they to do completed change listener.
-
2:40
Let's go ahead and look at what that is.
-
2:42
That is if an item is checked.
-
2:46
So if it changes from being not completed to completed that item will then
-
2:51
be called on, whatever implements this interface.
-
2:54
So we can see here that we have a check box for each item.
-
2:59
And the first thing we're going to do is actually set the on
-
3:02
check listener to normal.
-
3:03
That's because of this interesting thing with Android where if you
-
3:09
call a set checked on a check box and there's a listener set,
-
3:14
it's gonna get that call back with the new value even if you're just setting it for
-
3:19
the first time with an initial.
-
3:21
Item value so in this case we don't want to get the call back until the value
-
3:26
changes so we said to know we set the value then we set up our listener here.
-
3:32
And we'll get changes to do the listener, to do change listener.
-
3:37
Whenever an item is checked or unchecked.
-
3:40
We also have it on to do list change so
-
3:43
again this thing is also implementing ToDoListener.
-
3:46
So it's listening for changes to the list and
-
3:49
it's updating its data set whenever items in our to do list change as well.
-
3:55
And then let's go back to the main activity and
-
3:58
kinda see how this is all put together.
-
4:00
So here in our main activity, we have all the views,
-
4:04
which is mainly the RecyclerView, which has an adapter.
-
4:07
We also have the ability to add a new item to the list
-
4:11
via an input which is an EditText and then we have our list of data and our filter.
-
4:16
And we can see that we save our data and we serialize it to the bundle.
-
4:22
That's for rotation and handling other instances where our activities destroyed
-
4:26
and then recreated and we can also see that.
-
4:31
We set up a to do list filter with the list and
-
4:34
we set the filter to interact as a part of the listener.
-
4:39
With the to do adapter so there's all these listener's.
-
4:42
Calling to each other in each item and it's a little bit complicated.
-
4:47
But we're going to see that our next java helps us to simplify this
-
4:50
model quite a bit.
-
4:53
We can also see that we just look up our recycler view we set our adapter.
-
4:57
And we also have our Ability to add another todo item.
-
5:03
So in this case we have our ClickListener for a button to add the todo.
-
5:07
When we click that button we want to get the text.
-
5:11
Make sure it's not empty.
-
5:13
Add the item to the list.
-
5:14
Notify everything that the changes have occurred.
-
5:18
Set the input box back to nothing.
-
5:22
Request focus away from our input box and then dismiss the keyboard.
-
5:25
So this is basically going to dismiss the keyboard and refresh our list.
-
5:29
And that's gonna happen by triggering all of these listeners that are set up
-
5:33
between our adapter and our filter and our to do list.
-
5:37
We can also see that we have this spinner at the top in our toolbar, and
-
5:41
the spinner has three values.
-
5:44
It has the All, Completed, and Incomplete values.
-
5:47
And when we select one of those we also need to trigger all all those listeners to
-
5:53
change the value of what UI is being displayed.
-
5:56
What items get put in our adapter essentially, and we do that by
-
5:59
getting our filter and calling set filter mode to the position which is an indicator
-
6:04
of which filter we're using and we also have to notify our adapter.
-
6:10
Which needs to get the newest filtered data from our filter as well.
-
6:15
So again these are all kind of working together each of these is
-
6:19
tied to one another in all these listeners and how they are set up.
-
6:23
Now there's probably a better way to do this.
-
6:25
I know there is one at least which is using our X Java.
-
6:27
There are other ways to do this but this is a common thing you see in Android code
-
6:32
which is listeners, interfaces and then working together to inform
-
6:37
when data has changed or when the UI needs to be updated in some way.
-
6:42
So this is something you probably commonly see in Android code, it may be something
-
6:46
you've written before and will see in RX Java is that there's a much simpler
-
6:51
straight forward way to do it and something that's also less error prone.
-
6:57
Okay so the first step that we want to take when we're going to for
-
7:00
this project to use Rx-java is we need to add some dependencies on Rx-java and
-
7:06
some other libraries that will help us along the way.
-
7:08
So go ahead and go to your project and open up your build.gradle for
-
7:13
the app module, and in here what we'll see is at the very bottom, under dependencies,
-
7:19
we need to add some dependencies for our RX Java, that's the first one.
-
7:25
We have here RX Java one dot one dot zero.
-
7:28
You can get the latest version from the RX Java website.
-
7:32
But if you wanna use the code I'm using here make sure you use that same version.
-
7:38
If you use a different version, code might change.
-
7:41
And I'm also using rxandroid 1.1.0.
-
7:43
You'll notice that both of these have the exact same version number.
-
7:48
And that's gonna be the case going forward in the future, these should pretty much
-
7:51
have the same numbers because RX Android will be updated along with RX Java.
-
7:57
What you also see is RX Binding and what RX Binding does,
-
8:01
is it allows us to have observables and subscriptions with common
-
8:07
UI elements, views in Android and it's very helpful.
-
8:11
Comes in handy a lot of times.
-
8:13
So, arcs binding the default one is for
-
8:17
all the standard views that come with Androids STK.
-
8:19
And we also have bindings for
-
8:22
the support library apt compact design or cycle or view as well.
-
8:27
And then finally just because it's easier let's go ahead and
-
8:31
use butter knife as well.
-
8:32
That'll make it easier to look up our references to views and
-
8:36
you've probably used this before in multiple workshops and courses.
-
8:40
So you should be familiar with that.
-
8:42
Once you've added these you're gonna need to good ahead an sync so
-
8:44
we're good ahead sync now and
-
8:45
then our project should be ready to go to start adding in RX Java functionality.
You need to sign up for Treehouse in order to download course files.
Sign up